Show search bar in navigation bar without scrolling on iOS 11

后端 未结 4 1852
南方客
南方客 2020-12-04 13:00

I’m attaching a UISearchController to the navigationItem.searchController property of a UITableViewController on iOS 11. This works fine: I can use the nice iOS

相关标签:
4条回答
  • 2020-12-04 13:44

    The following makes the search bar visible at first, then allows it to hide when scrolling:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = false
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = true
        }
    }
    

    Using isActive didn't do what I wanted, it makes the search bar active (showing cancel button, etc.), when all I want is for it to be visible.

    0 讨论(0)
  • 2020-12-04 13:49

    On iOS 13, @Jordan Wood's answer didn't work. Instead I did:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIView.performWithoutAnimation {
            searchController.isActive = true
            searchController.isActive = false
        }
    }
    
    0 讨论(0)
  • 2020-12-04 13:51

    For me it worked after adding following lines in viewDidLoad() method:

    navigationController?.navigationBar.prefersLargeTitles = true
    navigationController!.navigationBar.sizeToFit()
    
    0 讨论(0)
  • 2020-12-04 13:58

    You can set the property isActive to true after adding the searchController to the navigationItem.

    Just like this:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchController.isActive = true
    }
    
    0 讨论(0)
提交回复
热议问题