ios - iOS 9 中的 UISearchController - UISearchBar 中的 TextField 剪切出导航项

标签 ios swift ios9 uisearchcontroller

我试图在 iOS 9 上的导航项上获得平滑的搜索栏,这意味着我无法使用 navigationItem.searchController 属性,因为它仅适用于 iOS 11。

class SearchContainerViewController: UITableViewController {
    let dataSource = ["1", "2", "3", "4", "5"]

    override public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = dataSource[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismiss(animated: true, completion: nil)
    }
}

class SearchViewController: UISearchController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class MyViewController : UIViewController, UISearchResultsUpdating, UISearchBarDelegate {
    lazy var searchButton = UIBarButtonItem(title: "Search", style: UIBarButtonItem.Style.plain, target: self, action: #selector(showSearchBar))

    var searchViewController: SearchViewController = {
        let container = SearchContainerViewController()
        let searchController = SearchViewController(searchResultsController: container)

        return searchController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupSearchController()
        setupSearchButton()
    }

    func setupSearchController() {
        searchViewController.searchResultsUpdater = self
        searchViewController.searchBar.delegate = self

        searchViewController.dimsBackgroundDuringPresentation = false
        searchViewController.hidesNavigationBarDuringPresentation = false
        searchViewController.searchBar.searchBarStyle = .minimal
        searchViewController.searchBar.showsCancelButton = true

        definesPresentationContext = true
    }

    @objc func showSearchBar() {
        UIView.animate(withDuration: 0.75) {
            self.navigationItem.titleView = self.searchViewController.searchBar
            self.navigationItem.rightBarButtonItem = nil
            self.searchViewController.searchBar.becomeFirstResponder()
        }
    }

    func setupSearchButton() {
        UIView.animate(withDuration: 0.75) {
            self.navigationItem.titleView = nil
            self.navigationItem.rightBarButtonItem = self.searchButton
        }
    }

    //  MARK: Conforms to UISearchResultUpdating

    public func updateSearchResults(for searchController: UISearchController) { }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        setupSearchButton()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        view.layoutSubviews()
    }
}


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let newWindow = UIWindow(frame: UIScreen.main.bounds)

        let mainViewController = MyViewController()
        let navigationController = UINavigationController(rootViewController: mainViewController)

        newWindow.backgroundColor = .white
        newWindow.rootViewController = navigationController
        newWindow.makeKeyAndVisible()

        window = newWindow

        return true
    }
}

虽然结果有点令人失望,因为带有 StatusBar 的 TextView 从导航项上下文中剪切出来,但我做错了什么并且可以做得更好吗?

感谢您的支持。

最佳答案

在人们无缘无故地否决这个问题之前,我将做一些不同的事情并回答我自己的问题。

发生剪裁的原因是,在这两种情况下,navigationItem 的高度都不同,因为如果将大内容放入 titleView(如搜索栏)中,它们在某种程度上是可拉伸(stretch)的。

我从一开始就在 navigationItem 上设置了 searchBar,并在应该完成时更改了其 isHidden 属性。

    @objc private func activateSearch() {
        UIView.animate(withDuration: 0.75) {
            self.navigationItem.titleView?.isHidden = false
            self.navigationItem.rightBarButtonItem = nil
            self.searchController.isActive = true
        }
    }

    private func deactivateSearch() {
        UIView.animate(withDuration: 0.75) {
            self.navigationItem.titleView?.isHidden = true
            self.navigationItem.rightBarButtonItem = self.searchButton
            self.searchController.isActive = false
        }
    }

关于ios - iOS 9 中的 UISearchController - UISearchBar 中的 TextField 剪切出导航项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386598/

相关文章:

ios - 如何从 FireBase 读取数据

ios - swift xcode 错误 : "Row.Type has no subscript members"

swift - 将时间字符串快速转换为日期

ios - Controller 中的重复代码

swift - 我们如何识别应用程序是否是通过应用程序横幅启动的?

swift - ReactiveCocoa 4 MVVM HTTP 请求最佳实践

ios - 重新加载表格 View 并向右滑动以删除

ios - 我无法从不同类上的函数调用 javascript

ios - 解析服务器(aws-mongolab)图像未检索

ios-防止滚动时重新加载 UITableView 中的单元格