为了了解 UISearchDisplayController 的工作原理,我在 viewDidLoad 方法中编写了以下代码:
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];
关于iphone - 为什么 UISearchBar 显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778416/