我创建SearchBarView
了 的子类UIView
,并将UISearchBar
其作为子视图添加到其中。(从)调用后removeFromSuperview
,不会消失。我试图从我创建 SearchBarView 的地方调用,但它没有帮助。任何想法为什么?SearchBarView
searchBarSearchButtonClicked
UISearchBar
removeFromSuperview
SearchBarView
#import <UIKit/UIKit.h>
@interface SearchBarView : UIView <UISearchBarDelegate> {
UISearchBar *querySearchBar;
}
@end
#import "SearchBarView.h"
@implementation SearchBarView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
querySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,frame.size.width,44)];
querySearchBar.delegate = self;
[self addSubview:querySearchBar];
}
return self;
}
#pragma mark -
#pragma mark UISearchBarDelegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self removeFromSuperview];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = @"";
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
return YES;
}
@end
SearchBarView 创建如下:
SearchBarView *searchBarView = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 0, 480, 300)];
UIView *rootView = [[[[UIApplication sharedApplication] delegate] window] rootViewController].view;
[rootView addSubview:searchBarView];