我想使用自定义UISearchBar和UISearchController作为导航标题来解决iOS 11的问题,即高度从44点变为56点。但我无法理解如何将自定义搜索栏设置为搜索控制器 - 搜索栏属性是只读的。我对这个主题的所有搜索都没有产生任何帮助我走上正确道路的环节。
这是我的搜索控制器代码:
注意:AppSearchControllerViewController是一个简单的UITableViewController,通过NSMutableArray显示结果
注2:视图控制器实现委托“UISearchBarDelegate”
-(void)viewDidLoad {
searchResultsController = [[AppSearchControllerViewController alloc] init];
appSearchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
[searchResultsController setParentNavigationController:self.navigationController];
appSearchController.searchResultsUpdater = self;
appSearchController.dimsBackgroundDuringPresentation = YES;
// Usually one would set up the searchbar like this
// self.navigationItem.titleView = appSearchController.searchBar;
// but instead I do this:
[self addSearchBar];
}
- (void)addSearchBar {
_searchBar = [self addSearchBarWithFrame:CGRectMake(0, 0, kScreenWidth - 2 * 44 - 2 * 15, 44)];
UIView *wrapView = [[UIView alloc] initWithFrame:_searchBar.frame];
[wrapView addSubview:_searchBar];
self.navigationItem.titleView = wrapView;
}
- (WMSearchBar *)addSearchBarWithFrame:(CGRect)frame {
self.definesPresentationContext = YES;
WMSearchBar *searchBar = [[WMSearchBar alloc]initWithFrame:frame];
searchBar.delegate = self;
searchBar.placeholder = [Language get:@"APP_SEARCH_NAVBAR_TEXTFIELD_SEARCH_PLACEHOLDER"];
[searchBar setShowsCancelButton:NO];
[searchBar setTintColor:KGenericColor];
if (self.isChangeSearchBarFrame) {
CGFloat height = searchBar.bounds.size.height;
CGFloat top = (height - 20.0) / 2.0;
CGFloat bottom = top;
searchBar.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
}
return searchBar;
}
WMSearchBar类来自此Github回购:https://github.com/DreamTravelingLight/searchBarDemo
这使得搜索栏再次完全适合44点,并且还可以在控制器转换上执行淡入/淡出动画。
现在的问题是,我收到搜索栏中的所有活动 - 一切正常。但搜索结果控制器未被触发,因此结果也不会显示。
我还尝试从我的自定义新搜索栏设置原始搜索栏的文本,但效果相同 - 结果控制器不会显示。如何连接新搜索栏并替换旧搜索栏,我需要做什么?
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"textDidChange: %@", searchText);
appSearchController.searchBar.text = searchText;
...