UISearchController 使用中遇到的各种问题

首先初始化,这就不细说了,三个代理都写上:

    _searchController.searchResultsUpdater = self;
    _searchController.searchBar.delegate = self;
    _searchController.delegate = self;

一、自动弹出键盘

以往的写法:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.searchController.active = true;
}

- (void)didPresentSearchController:(UISearchController *)searchController {
    [self.searchController.searchBar becomeFirstResponder];

}

实现如上的代码就可以了,但是如果把searchbar写到导航条上,发现这么写完全是没作用的。修改:

- (void)didPresentSearchController:(UISearchController *)searchController {
    [UIView animateWithDuration:0.1 animations:^{} completion:^(BOOL finished) {
        [self.searchController.searchBar becomeFirstResponder];
    }];
}

二、搜索输入时禁止searchBar上移隐藏导航条

 _searchController.hidesNavigationBarDuringPresentation = NO;

三、修改searchBar取消按钮的颜色和文字

 [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor whiteColor]];
 [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"取消"];

四、显示searBar的光标(如果不显示可以这么设置)

 _searchController.searchBar.tintColor = [UIColor blueberryColor];

 

posted @ 2017-05-17 17:55  MaricoSun  阅读(3870)  评论(0编辑  收藏  举报