简体   繁体   English

UISearchBar的范围栏未在iOS 7中显示?

[英]UISearchBar's scope bar is not showing in iOS 7?

I'm creating a UISearchViewController and I wish to put the UISearchBar in the navigation controller's navigation item. 我正在创建一个UISearchViewController ,我希望将UISearchBar放在导航控制器的导航项中。 Here's how I did it with no results: 这是我没有任何结果的方法:

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;

I also tried: 我也尝试过:

self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchDisplayController.searchBar.showsScopeBar = YES;

Am I missing out anything or doing something wrong here? 我在这里错过了什么还是做错了什么吗?

Edit: This and this is exactly what I'm aiming for. 编辑: 正是我的目标。

Try with following code 尝试以下代码

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Hashtags", nil];
searchBar.showsScopeBar = YES;
self.searchBar.delegate = self;

The scope bar will only appear when the search bar is active. 仅当搜索栏处于活动状态时,范围栏才会出现。 The search display controller is hiding the search bars scope bar. 搜索显示控制器正在隐藏搜索栏范围栏。

If you want the scope bar always available, you'll have to do a little hack work. 如果您希望范围栏始终可用,则必须做一些修改工作。 Maybe one of these will help. 也许其中之一会有所帮助。

How to have UISearchBar scope bar always visible? 如何使UISearchBar范围栏始终可见?

IOS7 : uisearchdisplaycontroller always show scope bar iOS7:uisearchdisplaycontroller总是显示范围栏

EDIT: 编辑:

Have you tried something in this direction? 您是否已尝试过此方向的操作? Assuming you have added the search bar to the navigation bar's titleView . 假设您已将搜索栏添加到导航栏的titleView

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGRect rect = self.navigationItem.titleView.superview.frame;
    rect.size.height = 88.0f;
    self.navigationItem.titleView.superview.frame = rect;
}

尝试调整searchBar框架:-

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake:(0,0,320,88)];

I set "Use Auto Layout" from xip inspector -> interface builder 我从xip检查器->界面构建器中设置了“使用自动布局”

Thats working perfectly with me 多数民众赞成与我完美

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM