[英]Position UISearchBar programmatically using UISearchController
我试图将我的UISearchBar直接放在UINavigationBar的下面,但是却出现了根本没有出现的问题。 我正在使用iOS8的新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标头中的方法,但是这样做的问题是它会随我所需要的表一起滚动。 我希望它在表格视图上方,因此它会粘住并尝试此操作,但是除非我没有使用正确的值,否则它现在似乎已经消失了?
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),所以它会被状态栏和导航栏隐藏。 如果更改为:您应该看到搜索栏:
self.searchController.searchBar.frame = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44.0);
我有两个解决方案。 我将向您展示一种简单的方法(您也可以使用情节searchBarView
):在ViewController中创建一个名为searchBarView
的UIView; 将self.searchController.searchBar
添加到searchBarView
; 将searchBarView
添加到您的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()
。 如果它是通用应用程序,则需要添加此方法(至少在我看来是要使所有功能正常运行):
- (void)viewDidLayoutSubviews {
// you could check
// if (IS_IPAD || (IS_IPHONE_6PLUS && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)))
[self.searchController.searchBar sizeToFit];
}
这是我对stackoverflow的第一个答案。 希望这个帮助^^。
要检查的两件事:
尝试在UIViewController
上将UIViewController
automaticallyAdjustsScrollViewInsets
设置为false
。 这会将视图控制器视图的子视图移动到导航栏下方。
使用searchFieldBackgroundPositionAdjustment
(适用于iOS 5+)在UISearchBar
内调整搜索字段的位置
用法:
let searchBar = UISearchBar()
searchBar.searchFieldBackgroundPositionAdjustment = UIOffset(horizontal: 0, vertical: 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.