简体   繁体   中英

iOS : Resizing subview by removing a subview

  • I have a view which contains 3 subivews called view A , view B , view C .
  • When I remove a subview means I need to set margin automatically to adjust.
  • When I remove view A means, remaining views have to move up automatically.

Note: Not in autolayout . Please tell how its possible in automask

在此处输入图片说明

It will not work automatically, you need to program a bit, you can try the following code to achieve this,

//Adding Delete Tap Gesture
-(void)addGestureToSubViews{
    for(UIView *view in parent.subviews){
        UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteAction:)];
        [view addGestureRecognizer:gesture];
    }
}


-(IBAction)deleteAction:(UITapGestureRecognizer *)sender{
    UIView *view=sender.view;

    [UIView animateWithDuration:.3 animations:^{
        CGRect rect=view.frame;
        rect.origin.x=view.superview.frame.size.width;
        view.frame=rect;
    } completion:^(BOOL finished) {
        [self reArrangeSuperView:view.superview withDeletedViewFrame:view.frame];
        [view removeFromSuperview];

    }];

}

-(void)reArrangeSuperView:(UIView *)superView withDeletedViewFrame:(CGRect)frame{

    for(UIView *view in superView.subviews){
        CGRect rect=view.frame;

        if(rect.origin.y>frame.origin.y){
            rect.origin.y=frame.origin.y;
        }

        [UIView animateWithDuration:.3 animations:^{
            view.frame=rect;
        }];

    }
}

I hope it helps.

Cheers.

为此,您必须在属性检查器中隐藏视图,并且要向上推哪个视图,应设置视图的-contentoffset属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM