菜鸟教程小白 发表于 2022-12-11 18:45:44

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


                                            <p><p>我正在尝试向我的导航 Controller 添加两个按钮和一个 SearchBar,但对齐不正确。我已经尝试了几乎所有方法,例如更改 SearchBar、UIButton 的大小或更改 Y 定位,但没有奏效。知道如何解决这个问题吗?
<a href="/image/neNOO.png" rel="noreferrer noopener nofollow"><img src="/image/neNOO.png" alt="enter image description here"/></a>
我正在使用 iOS 11 和 Xcode 9</p>

<pre><code>    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: &#34;settings-button&#34;), 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()
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>请检查:</p>

<pre><code>override func viewWillLayoutSubviews() {
    searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
    searchController?.searchBar.sizeToFit()
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 导航 Controller 中的 UISearchBar 和两个按钮,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46383888/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46383888/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 导航 Controller 中的 UISearchBar 和两个按钮