美文网首页
UISearchController踩坑奇遇记

UISearchController踩坑奇遇记

作者: he15his | 来源:发表于2017-11-24 16:05 被阅读1113次

    最近由于优化搜索功能,要把之前工程内废弃的UISearchDisplayController换成UISearchController,总结下期间遇到的一些问题,便于以后查找,分享出来让大家少踩坑。

    使用方法

    1.创建

    建议如下设置ViewController和tableView,手动管理tableView偏移

        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.definesPresentationContext = YES;//不设置会导致一些位置错乱,无动画等问题
    

    创建UISearchController

    - (UISearchController *)searchController {
        if (!_searchController) {
            //创建方法,如果在当前控制器显示,传nil
            _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
            //结果代理回调
            _searchController.searchResultsUpdater = self;
            //可选,searchBar的代理回调
            //_searchController.searchBar.delegate = self;
            //可选,回调SearchController的显示退出等
            //_searchController.delegate = self;
            //active状态是否显示一个半透明的覆盖层
            if (@available(iOS 9.1, *)) {
                _searchController.obscuresBackgroundDuringPresentation = NO;
            }
            _searchController.dimsBackgroundDuringPresentation = NO;
            //active状态是否隐藏navigationBar,默认为YES
            _searchController.hidesNavigationBarDuringPresentation = NO;
        }
        return _searchController;
    }
    
    

    2.UI显示

    • 作为UITableView的tableHeaderView

    •   self.tablView.tableHeaderView = self.searchController.searchBar;
      
    • 添加到UINavigationBar

    •   self.navigationItem.titleView = self.searchController.searchBar;
      
    • 添加到其它view

    •   [self.view addSubview:self.searchController.searchBar];
      

    3.搜索数据显示

    实现- (void)updateSearchResultsForSearchController:(UISearchController *)searchController代理方法,需要更新数据时会回调到这个方法,然后在里面做搜索操作,最后刷新列表。

    如果搜索前的数据和搜索后的数据是在一个UITablView上显示的,在UITableView的代理回调里,可通过self.searchController.active判断应该显示原数据还是搜索后的数据

    自定义技巧

    获取输入框

    UITextField *textField = [searchBar valueForKey:@"_searchField"];

    获取取消按钮

    UIButton *cancelButton = [searchBar valueForKey:@"_cancelButtonText"];

    总是不显示取消按钮

    继承UISearchBar,重写show方法,不做任何处理

    class KDSearchBar: UISearchBar {
        
        var alwaysHiddenCancelButton: Bool = false
        override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
            if !alwaysHiddenCancelButton {
                super.setShowsCancelButton(showsCancelButton, animated: animated)
            }
        }
    }
    

    继承UISearchController,重写SearchBar的Getter方法

    class KDSearchController: UISearchController {
        
        fileprivate let reallySearchBar = KDSearchBar()
        ///总不显示取消按钮,默认searchBar点击时会触发显示
        var alwaysHiddenCancelButton: Bool = false {
            didSet {
                reallySearchBar.alwaysHiddenCancelButton = alwaysHiddenCancelButton
            }
        }
        override var searchBar: UISearchBar {
            return reallySearchBar
        }
    }
    

    自定义输入框高度

    override func layoutSubviews() {
            super.layoutSubviews()
            
            //设置textfield
            if let textField: UITextField = value(forKey: "_searchField") as? UITextField {
                var frame = textField.frame
                frame.size.height = 30
                frame.origin.y = (self.bounds.size.height - frame.size.height) / 2.0
                textField.frame = frame
                textField.layer.cornerRadius = 15
                textField.layer.masksToBounds = true
            }
    }
    

    奇遇记

    iOS8问题汇总

    1. updateSearchResultsForSearchController不回调

    如果设置了searchBar.delegate输入文字时不会调这个方法,只有进入active状态和退出active状态的时候会回调,解决办法就是实现- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText回调方法进行搜索

    2.UISearchBar不显示

    在合适的地方调用[self.searchController.searchBar sizeToFit];

    3.点击SearchBar不能移动到NavigationBar,点击取消后回不了原始状态

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
       self.searchController.active = YES; 
       return YES;
    }
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        self.searchController.active = NO; 
    }
    
    

    4.present一个VC后进入active状态点取消,页面退出模态视图了

    如果主视图是present过去的,不能设置active为NO,只能用[searchBar resignFirstResponder]代替

    5.active状态调用resignFirstResponder退出输入状态后点击取消失效

    这个时间点击取消发现并没有回调searchBarCancelButtonClicked而是回调到searchBarShouldBeginEditing,经调试发现取消按钮的enable值为NO,解决办法是使用KVO监听这个值,然后把这个值改为YES。

    6.active状态 tableview位置偏移20px

    如果原数据和搜索出的数据显示在一个列表上,在搜索状态tableView会上移20象素解决办法如下

    - (void)didPresentSearchController:(UISearchController *)searchController {
        //防止搜索时tableview位置不对
        [_tableView remakeConstraints:^(MASConstraintMaker *make){
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.view.top).with.offset(20);
        }];
    }
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        //调整frame写到这儿是因为界面跳转前先改回来,系统会自动生成对应的动画,不会跳得很厉害,不信你改到didDismissSearchController试试,你会回来谢谢我
        [_tableView remakeConstraints:^(MASConstraintMaker *make){
            make.edges.equalTo(self.view);
        }];
        //回滚到最初位置
        [_tableView setContentOffset:CGPointMake(0, 0)];
    }
    
    

    iOS11列表contentsize偶尔算不对

        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    

    不要问我为什么上面又是OC又是Swift,说多了都是泪-------混编

    相关文章

      网友评论

          本文标题:UISearchController踩坑奇遇记

          本文链接:https://www.haomeiwen.com/subject/qprfbxtx.html