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

问题描述 投票:0回答:1

我试图在iOS 9上的导航项上获得一个平滑的搜索栏,这意味着我不能使用qazxsw poi属性,因为它只有iOS 11。

navigationItem.searchController

虽然结果有点令人失望,因为StatusBar的textview是剪切出导航项上下文,有什么我做错了,可以做得更好吗?

感谢您的支持。

ios swift ios9 uisearchcontroller
1个回答
0
投票

在人们无缘无故地投票之前,我会做一些不同的事情并回答我自己的问题。

裁剪的发生是因为在两种情况下,navigationItem的高度都不同,因为如果在titleView中放入大内容(如searchBar),它们有些可伸缩。

我已经从navigationItem的开头设置了searchBar,只是在应该完成时将它们的isHidden属性设置为。

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.