我设法解决一个非常类似的问题给你通过编程设置UISearchController
像这样经历了一个:
注〜如果hidesNavigationBarDuringPresentation
没有被设置为NO的错误仍然存在。
#pragma mark - View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.results = [[NSArray alloc] init];
// 1 \\ Results Table View
UITableView *searchResultsTableView = [[UITableView alloc] initWithFrame:self.tableView.frame];
[searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];
searchResultsTableView.delegate = self;
searchResultsTableView.dataSource = self;
// 2 \\ init search results table view & setting its table view
self.searchResultsTableViewController = [[UITableViewController alloc] init];
self.searchResultsTableViewController.tableView = searchResultsTableView;
self.searchResultsTableViewController.view.backgroundColor = [UIColor blackColor];
// 3 \\ init a search controller with it's tableview controller for results
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsTableViewController];
self.searchController.hidesNavigationBarDuringPresentation = NO; // √
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor blackColor];
// 4 \\ Make an appropriate search bar (size, color, attributes) and add it as the header
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor];
// 5 \\ Enable presentation context
self.definesPresentationContext = YES;
self.tableView.backgroundColor = [UIColor clearColor];
self.currentUser = [PFUser currentUser];
}
增加更多详情:
- Interface Builder中:的UIViewController(不UITableViewController中)
添加UITableView
与约束:
一个。 
b。 *我的导航栏是定制的高度 - 你的“顶层空间到:顶面布置指南”可以改变
从添加表视图中添加代表&数据源网点到的UIViewController
一。不要忘了这样做编程:
@interface AddUsersViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
在你接口扩展:
一个。设置你的UISearchConroller
的委托和数据源:
#import "AddUsersViewController.h"
#define ResultsTableView self.searchResultsTableViewController.tableView
#define Identifier @"Cell"
@interface AddUsersViewController() <UISearchControllerDelegate, UISearchResultsUpdating>
@property (nonatomic) UISearchController *searchController;
@property (nonatomic) UITableViewController *searchResultsTableViewController;
@property (nonatomic, weak) UIBarButtonItem *leftBarButton;
@property (nonatomic) NSArray *results;
@end
虽然这个工作对我来说,我已经发现,有可能会造成这个其他可能出现的情况。我的“问题”并不是搜索栏向下移动了20px(或左右),而是移动了。我将其归因于包含图像标题和自定义NavBarButtons的自定义UINavigationBar标题。考虑到这一点,这里有两种可能的解决方案:
。
原方案:
你的容器视图的extendedLayoutIncludesOpaqueBars
物业
一个。 故事板:检查√在故事板中的表视图“不透明酒吧”下。
b。 以编程方式:yourTableView(s).extendedLayoutIncludesOpaqueBars
= YES;
。
设置scopeButtonTitles
到空数组。 (如下图)
self.searchController = UISearchController(searchResultsController: nil);
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.scopeButtonTitles = [];
self.tableView.tableHeaderView = self.searchController.searchBar;
后两种答案来自THIS问题,可能被证明是太检查出有益的。
感谢BananaNeil的研究和测试,他发现如果您按照我的建议设置searchController.searchBar.y = 0
,那么您的搜索栏应该完全位于您期望的位置。 √
我在这个问题'UISearchController'你还记得,如果/您是如何解决的呢? – AnthonyM