UISearchBar不适合iPad上的UINavigationBar - iOS 8

时间:2014-11-10 17:01:33

标签: objective-c ipad uisearchbar titleview uisearchcontroller

我正在尝试将UISearchBar放在UINavigationBar的titleView中。

我有两个解决方案,都不方便。

首先是:

[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:searchResultsController]];
[self.searchController setSearchResultsUpdater:searchResultsController];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[self.searchController setDelegate:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController.searchBar setDelegate:self];
[self.navigationItem setTitleView:self.searchController.searchBar];
[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setShowsCancelButton:YES];

不搜索时,结果看起来没问题。

Not searching mode

但是当搜索开始时,titleView在右侧扩展太多了。确实,取消按钮是可见的,但不在屏幕上......

enter image description here

this answer中,我尝试将UISearchBar包装在另一个UIView中,然后再将其添加到navigationItem。

[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:searchResultsController]];
[self.searchController setSearchResultsUpdater:searchResultsController];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[self.searchController setDelegate:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController.searchBar setDelegate:self];

[self.searchController.searchBar setShowsCancelButton:NO];
[self.searchController.searchBar sizeToFit];
UIView *titleViewWrapper = [[UIView alloc] initWithFrame:self.searchController.searchBar.frame];
[titleViewWrapper addSubview:self.searchController.searchBar];
[self.navigationItem setTitleView:titleViewWrapper];

[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setShowsCancelButton:YES];

结果如下:

不搜索:

enter image description here

搜索:

enter image description here

我正在管理左/右栏按钮项目,显示/隐藏自己在UISearchController的委托方法中。

这种情况发生在模拟器中,仅在iPad上发生。

有什么想法吗?

0 个答案:

没有答案