iphone - 为什么 UISearchBar 显示不正确?

标签 iphone objective-c ios cocoa-touch

为了了解 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/

相关文章:

ios - 使用 MMWormhole 库和 Watchkit 的 XCode 6.2 中的链接器命令失败

ios - 在 iOS 中比较两个频谱图

ios - UITextView 将可编辑属性的链接检测为 true

ios - 如何将选定的 UIActionSheet 按钮的标题放入标签中?

ios - 如何创建一个占据整个屏幕并提供 'dimming' 效果的 UIView?

ios - Firebase 登录用户在 segue 之前加载速度不够快

iphone - 如何 POST 请求以获得 JSON 响应

ios - locationManager 不适用于 iOS 9

ios - Objective C 中的表单验证

iphone - 用 iPhone 覆盖@media CSS