如何以编程方式快速修复搜索栏

时间:2018-12-22 09:33:29

标签: ios swift

我以编程方式在ViewController中创建了一个搜索栏。 但是当我向上滚动屏幕时,搜索栏就会上升。

如何冻结搜索栏?

class searchbarTable:  UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
    @IBOutlet var tableview: UITableView!

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        self.tableview.tableHeaderView = searchController.searchBar
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试此操作,并在导航栏中设置搜索栏

self.navigationController?.searchDisplayController = searchController

OR

lazy var searchBar = UISearchBar(frame: CGRectZero)

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.placeholder = "Search"

    navigationItem.titleView = searchBar
}

OR

var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
  self.navigationItem.leftBarButtonItem = leftNavBarButton

OR

searchController.hidesNavigationBarDuringPresentation = false

当我们显示还可以时,它会隐藏导航栏

希望它对您有帮助:)