我创建了一个 UIView。在视图上添加了一个搜索栏作为子视图。搜索栏也是通过代码创建的。
我已经为视图提供了一个 UIAnimation 以获得类似下拉菜单的效果。
到目前为止,它工作正常。
问题是当我删除超级视图时,视图消失但搜索栏仍然存在。
以下是一些代码:
@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;
}
}
添加子视图
删除子视图后