OGeek|极客世界-中国程序员成长平台

标题: ios - 导航 Controller 中的 UISearchBar 和两个按钮 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:45
标题: ios - 导航 Controller 中的 UISearchBar 和两个按钮

我正在尝试向我的导航 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()



Best Answer-推荐答案


请检查:

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4