ios - UISearchBar 和导航 Controller 中的两个按钮

标签 ios swift uinavigationcontroller uinavigationbar swift4

我正在尝试向我的导航 Controller 添加两个按钮和一个 SearchBar,但对齐方式不正确。我几乎尝试了所有方法,例如更改 SearchBar、UIButton 的大小或更改 Y 定位,但都没有用。知道如何解决这个问题吗? enter image description here 我正在使用 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()

最佳答案

请检查:

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

关于ios - UISearchBar 和导航 Controller 中的两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46383888/

相关文章:

iphone - UINavigationController 偏移量

ios - 如何设置 UITableViewRowAction 的 Action

ios - Xcode 中的 "Waiting to attach to [app name] on [device name]"消息

ios - 更新后在 Firebase 中查找项目的旧值

ios - 使用 KeyPath 编辑效果属性

ios - UINavigationController: viewDidLoad 被调用但不影响 UI

iphone - 核心数据排序并且不显示重复项

ios - 在 iOS 应用程序中实现全双工连接有哪些选项?

ios - 在跳过一个 View Controller 时导航 - iOS

ios - 当 segue 甚至 NavigationController Swift 时,TabBar 消失