简体   繁体   English

在iPad中删除超级视图时,子视图未删除

[英]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. 我创建了一个UIView。一个搜索栏被添加为视图的子视图。搜索栏也是通过代码创建的。

I have given a UIAnimation to the view to get a dropdown like effect to the view. 我给视图赋予了UIAnimation以获得像视图一样的下拉菜单。

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 删除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. 如果你想在searchBar要与一起修剪dropView然后设置dropView的clipToBounds为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. 但是我认为您不允许编辑UISearchBar的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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