我放棄了使用UITableViewController
,我解決了使用標準UIViewController
頂部UISearchBar
和它下面的UITableView
。通過設置self.edgesForExtendedLayout = UIRectEdgeNone
當搜索開始UISearchBar
不重疊的狀態欄:
-(id) initWithTableViewStyle: (int) tableViewStyle
{
self = [super init];
if (self) {
//Set the UITableViewStyle
self.tableViewStyle = tableViewStyle;
//Be sure the searchBar won't overlap the status bar
self.edgesForExtendedLayout = UIRectEdgeNone;
//Add the subviews to the mainView
[self.view addSubview:self.searchBar];
[self.view addSubview:self.tableView];
//Autolayout
//Create the views dictionary
NSDictionary *viewsDictionary = @{@"searchBar":self.searchBar,
@"tableView": self.tableView};
//Create the constraints using the visual language format
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"H:|[searchBar]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"H:|[tableView]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[searchBar(==44)][tableView]|"
options:0
metrics:nil
views:viewsDictionary]];
}
return self;
}
-(UITableView*) tableView
{
if (!_tableView){
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,0,0)
style:self.tableViewStyle];
_tableView.translatesAutoresizingMaskIntoConstraints=NO;
_tableView.delegate = self;
_tableView.dataSource=self;
}
return _tableView;
}
-(UISearchBar*) searchBar
{
if(!_searchBar){
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,0,0)];
_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
_searchBar.translatesAutoresizingMaskIntoConstraints=NO;
_searchBar.translucent = NO;
_searchBar.delegate = self;
}
return _searchBar;
}
的UIViewController中應作出:
UIViewController <NSFetchedResultsControllerDelegate,
UISearchBarDelegate,
UISearchDisplayDelegate,
UITableViewDataSource,
UITableViewDelegate>