我正在创建UISearchViewController
,我希望将UISearchBar
放在导航控制器的导航项中。这是我如何做到这一点没有结果:
self.searchBar = [UISearchBar new];
self.searchBar.showsScopeBar = YES;
self.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;
我也尝试过:
self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchDisplayController.searchBar.showsScopeBar = YES;
我错过了什么或做错了什么?
答案 0 :(得分:1)
尝试使用以下代码
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Hashtags", nil];
searchBar.showsScopeBar = YES;
self.searchBar.delegate = self;
答案 1 :(得分:1)
范围栏仅在搜索栏处于活动状态时显示。搜索显示控制器正在隐藏搜索栏范围栏。
如果您希望示波器栏始终可用,则您必须进行一些小工作。也许其中一个会有所帮助。
How to have UISearchBar scope bar always visible?
IOS7 : uisearchdisplaycontroller always show scope bar
编辑:
你有没有尝试过这方面的事情?假设您已将搜索栏添加到导航栏titleView
。
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect rect = self.navigationItem.titleView.superview.frame;
rect.size.height = 88.0f;
self.navigationItem.titleView.superview.frame = rect;
}
答案 2 :(得分:0)
尝试调整searchBar框架: -
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake:(0,0,320,88)];
答案 3 :(得分:0)
我设置"使用自动布局"来自xip inspector - >界面构建器
这与我完美合作