为什么UISearchBar显示不正确?
问题描述:
为了了解UISearchDisplayController是如何工作的,我写在viewDidLoad方法如下代码:为什么UISearchBar显示不正确?
UISearchBar * searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)] autorelease];
searchBar.delegate = self;
searchBar.showsCancelButton = YES;
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
[_searchController setActive:YES animated:YES];
[_searchController.searchBar becomeFirstResponder];
但是当我运行我的代码的UISearchBar不被显示,你可以在图片中看到。
答
创建UISearchBarcontroller它不错,但在UIViewController中没有添加。所以你可以在uiviewcontroller中添加UISearchBarcontroller。
编码:
[self.view addsubview:_searchController];