简体   繁体   中英

Subviews are not removing while removing the superview in iPad

I created a UIView.A search bar is added as subview on the view.The search bar is also created through code.

I have given a UIAnimation to the view to get a dropdown like effect to the view.

Upto this point it is working fine.

The problem is when i remove the superview the view is go but the search bar is still there.

Here are some codes:

@interface ATViewController : UIViewController<UISearchBarDelegate>
{
    UIView *dropView;

    UISearchBar *searchBar;
}

- (void)viewDidLoad
{
    dropView = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 150, 100)];
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,150,0)];
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)buttonClicked:(id)sender{
   // self.searchBar.delegate = self;
    [dropView addSubview:searchBar];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.9];

    dropView.backgroundColor = [UIColor blueColor];
    dropView.frame = CGRectMake(70, 70, 150, 550);
    self.searchBar.frame=CGRectMake(0,0, 150, 40);

    [UIView commitAnimations];
    [self.view addSubview:dropView];
}
-(void)doSingleTap:(UITapGestureRecognizer *)sender
{
    switch (sender.numberOfTapsRequired) {
        case 1:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];

            dropView.frame = CGRectMake(70, 70, 150, 0);
            self.searchBar.frame=CGRectMake(0, 0, 150, 0);
            [UIView commitAnimations];
            break;

        default:
            break;
    }

}

Adding the subview

添加子视图

After removing the subView

删除视图后

You are not removing the super view in code. You are just reducing its height to zero. If you want the searchBar to be clipped along with the dropView then set dropView 's clipToBounds to YES. ie;

dropView.clipToBounds = YES:

before you reduce its height to zero.

Noticed that you are reducing the search bars height to zero as well. But I think you are not allowed to edit the height of the UISearchBar.

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