为什么UISearchBar没有正确显示?

时间:2012-08-02 13:26:22

标签: 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没有显示,因为你可以在图像中看到。

1 个答案:

答案 0 :(得分:1)

您可以创建UISearchBarcontroller,但不会在uiviewcontroller中添加。所以你在uiviewcontroller中添加了UISearchBarcontroller。

编码:

[self.view addsubview:_searchController];