【问题标题】:UISearchBar and Two buttons in the Navigation Controller导航控制器中的 UISearchBar 和两个按钮
【发布时间】:2017-09-23 20:15:12
【问题描述】:

我正在尝试向我的导航控制器添加两个按钮和一个 SearchBar,但对齐不正确。我已经尝试了几乎所有方法,例如更改 SearchBar、UIButton 的大小或更改 Y 定位,但没有奏效。知道如何解决这个问题吗? 我正在使用 iOS 11 和 Xcode 9

    searchController = UISearchController(searchResultsController: nil)
    searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 200, height: 30)
    searchController?.delegate = self
    searchController?.searchResultsUpdater = self

    let refineButton = UIButton.init(type: UIButtonType.custom)
    refineButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    refineButton.setImage(#imageLiteral(resourceName: "settings-button"), for: UIControlState.normal)
    refineButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
    refineButton.heightAnchor.constraint(equalToConstant: 30).isActive = true


    let refineItem = UIBarButtonItem(customView: refineButton)


    navigationItem.leftBarButtonItem = refineItem
    navigationItem.titleView = searchController?.searchBar
    searchController?.searchBar.sizeToFit()

【问题讨论】:

  • 我不确定这是否会有所帮助,但为什么不使用navigationItem.searchController = searchController 而不是navigationItem.titleView = searchController?.searchBar。当然,这只适用于 iOS 11。
  • @rmaddy 不幸的是,这不起作用,因为搜索控制器将位于按钮下方

标签: ios swift uinavigationcontroller uinavigationbar swift4


【解决方案1】:

请检查:

override func viewWillLayoutSubviews() {
    searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
    searchController?.searchBar.sizeToFit()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2011-11-29
    • 2019-05-04
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多