我想在导航栏的右侧添加一个由UISearchController管理的搜索栏。

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.searchBarStyle = .Minimal
    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 200.0, height: 44.0)
    searchController.hidesNavigationBarDuringPresentation = false
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchController.searchBar)

当我点击搜索栏(使其变为 Activity 状态)时,它覆盖了整个导航栏(同时重叠了其他所有内容),而不是一小部分。如何固定框架?

最佳答案

在UIView中包装搜索栏似乎可以解决问题。

    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 200.0, height: 44.0)
    let barContainer = UIView(frame: searchController.searchBar.frame)
    barContainer.backgroundColor = UIColor.clearColor()
    barContainer.addSubview(searchController.searchBar)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: barContainer)

关于ios - 如何将搜索栏添加到导航栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39212072/

10-11 22:32