在 iOS 8 中,UISearchController 与 UINavigationController 交互时出现了奇怪、随机、不一致的行为。选择搜索栏后,iOS 有时会显示右侧的视图,有时会显示左侧的视图:
您可以在 YouTube 上更好地看到它的动态说明。
我不在乎我得到哪个版本,但正是这种不一致让我发疯。
这viewDidLoad
是设置搜索栏和工具栏的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Create our 4 view-filter buttons & a flex space
self.mainButton = [[UIBarButtonItem alloc] initWithTitle:@"Main" style:UIBarButtonItemStylePlain target:self action:@selector(viewMain)];
self.viewedButton = [[UIBarButtonItem alloc] initWithTitle:@"Viewed" style:UIBarButtonItemStylePlain target:self action:@selector(viewViewed)];
self.favoritesButton = [[UIBarButtonItem alloc] initWithTitle:@"Favorites" style:UIBarButtonItemStylePlain target:self action:@selector(viewFavorites)];
self.allButton = [[UIBarButtonItem alloc] initWithTitle:@"All" style:UIBarButtonItemStylePlain target:self action:@selector(viewAll)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// Add toolbar buttons
NSArray *rightSideButtons = @[self.mainButton, flexSpace, self.favoritesButton, flexSpace, self.viewedButton, flexSpace, self.allButton];
[self setToolbarItems:rightSideButtons];
[self.navigationController setToolbarHidden:FALSE animated:FALSE];
// Set up search bar
self.searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.searchResultsController.tableView.dataSource = self;
self.searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.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.tableView.tableHeaderView = self.searchController.searchBar;
// this tells the search controller where to display
self.definesPresentationContext = YES;
}
知道为什么 iOS 似乎在显示工具栏或用键盘覆盖它之间随机切换吗?如果您需要更多代码,请告诉我,但我发布了所有看似相关的部分。
谢谢!