【问题标题】:How to add a search bar to the navigation bar如何在导航栏中添加搜索栏
【发布时间】:2016-08-29 17:40:14
【问题描述】:

我想在导航栏的右侧添加一个由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)

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

【问题讨论】:

  • 您是否尝试将UIView 从对象库拖放到导航栏,然后将UISearchController 拖放到该 UIView 中? ...我没有尝试过,所以我不确定它是否应该工作。
  • UISearchController 在对象面板中不可用。但是将搜索栏包裹在 UIView 中似乎是个好主意,让我看看。
  • 很好,将搜索栏包装在 UIView 中就可以了。谢谢你:)

标签: ios swift ipad uikit uisearchcontroller


【解决方案1】:

在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多