使用 UISearchController 以编程方式定位 UISearchBar [英] Position UISearchBar programmatically using UISearchController

查看:24
本文介绍了使用 UISearchController 以编程方式定位 UISearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的 UISearchBar 直接放在我的 UINavigationBar 下,但我遇到了根本没有出现的问题.我正在使用 iOS8 的新 searchController.

I'm attempting to position my UISearchBar directly under my UINavigationBar but I'm having issues where it's not appearing at all. I'm using iOS8's new searchController.

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
 self.searchController.searchResultsUpdater = self;
 self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
 self.salesTableView.tableHeaderView = self.searchController.searchBar;

目前我正在使用它正确地将它添加到我的 UITableView 标题,但问题是它随表格滚动,这不是我想要的.我希望它在我的 tableview 上方,所以它会粘住并尝试过这个,但它现在似乎已经消失了,除非我没有使用正确的价值?

Currently I'm using that which correctly adds it to my UITableView header, but the issue with that is that it scrolls with the table which is not what I want. I want it above my tableview so it sticks instead and tried this but it seems to be gone now, unless I'm not using the right values?

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
 self.searchController.searchResultsUpdater = self;
 self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
 //  self.salesTableView.tableHeaderView = self.searchController.searchBar;

推荐答案

因为原点是(0,0),所以会被状态栏和导航栏隐藏.如果更改为:

Because the origin is (0,0), it will be hidden by status bar and navigation bar. You should see the search bar if changing to:

self.searchController.searchBar.frame = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44.0);

我有两个解决方案.我会告诉你一个简单的方法(你也可以使用故事板):在你的 ViewController 中创建一个 UIView,命名为 searchBarView;在searchBarView中添加self.searchController.searchBar;将 searchBarView 添加到您的 ViewController.

I have two solution. I will show you the easy way (you could also use storyboard): Create a UIView in your ViewController, named searchBarView; Add self.searchController.searchBar into the searchBarView; Add searchBarView to your ViewController.

UIView *searchBarView = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44);
[searchBarView addSubView:self.searchController.searchBar];
[self.view addSubView:searchBarView];

我假设您在 viewDidload 中设置了搜索控制器 self.searchController.searchBar.sizeToFit().如果是通用应用程序,则需要添加此方法(至少在我的情况下可以使一切正常):

I assume that you setup search controller self.searchController.searchBar.sizeToFit() in viewDidload. If it is the universal app, you need to add this method (at least in my case to make everything work):

- (void)viewDidLayoutSubviews {
    // you could check
    // if (IS_IPAD || (IS_IPHONE_6PLUS && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))) 
    [self.searchController.searchBar sizeToFit];
}

这是我对 stackoverflow 的第一个回答.希望这有助于^^.

This is my first answer on stackoverflow. Hope this help ^^.

这篇关于使用 UISearchController 以编程方式定位 UISearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆