我正在尝试将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];
不搜索时,结果看起来没问题。
但是当搜索开始时,titleView在右侧扩展太多了。确实,取消按钮是可见的,但不在屏幕上......
在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];
结果如下:
不搜索:
搜索:
我正在管理左/右栏按钮项目,显示/隐藏自己在UISearchController的委托方法中。
这种情况发生在模拟器中,仅在iPad上发生。
有什么想法吗?