完美解决UISearchController所有坑!

UISearchController的初始化,自不必多说:

       searchVC.searchBar = searchController.searchBar
        if #available(iOS 11.0, *) {
            navigationItem.searchController = searchController
        } else {
            tableView.tableHeaderView = searchController.searchBar
            tableView.scrollToRow(at: IndexPath.init(row: 0, section: 0), at: .top, animated: false)
        }

// MARK: - 搜索
    lazy var searchController: UISearchController = {
        let vc = UISearchController.init(searchResultsController: searchVC)
        vc.searchResultsUpdater = searchVC
        vc.delegate = self
        vc.searchBar.delegate = self
        vc.searchBar.sizeToFit()
        vc.dimsBackgroundDuringPresentation = false
        return vc
    }()

searchBar向上偏移?在当前控制器设置:

self.definesPresentationContext = true

重点来了,也是很多填坑文章始终没解决的问题,searchBar向下偏移64?
大部分都是用了第三方键盘处理工具IQKeyboardManager,所以在当前控制器禁用掉:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //键盘
        IQKeyboardManager.shared.enable = false
    }

至于其他的,都能在其他填坑类文章找到答案,就不一一列举。

你可能感兴趣的:(完美解决UISearchController所有坑!)