菜鸟教程小白 发表于 2022-12-11 18:20:01

ios - 搜索栏在点击时隐藏


                                            <p><p>您好,我在 UIView 上添加了搜索栏。当我运行我的代码时,我可以看到我的搜索栏,但是当我在搜索栏中点击它时它会隐藏起来,当我再次点击屏幕上的某个地方时它是可见的。我现在没有遇到这个问题。请帮忙。</p>

<pre><code> var searchView:UIView = {
      var search = UIView()
      search.translatesAutoresizingMaskIntoConstraints = false
      search.backgroundColor = UIColor.gray
      return search
    }()

   lazy var searchController : UISearchController = {
      var searchController = UISearchController(searchResultsController: nil)
      //searchController.searchResultsUpdater = self
      searchController.hidesNavigationBarDuringPresentation = false
      searchController.searchBar.barTintColor = UIColor.gray
      searchController.searchBar.layer.borderWidth = 1
      searchController.searchBar.layer.borderColor = UIColor.gray.cgColor
      //searchController.dimsBackgroundDuringPresentation = false
      searchController.definesPresentationContext = true
      searchController.searchBar.sizeToFit()
      searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
      return searchController
    }()
func setUpView(){
    view.addSubview(searchView)
    searchView.addSubview(searchController.searchBar)

    searchView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    searchView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    searchView.topAnchor.constraint(equalTo: view.topAnchor,constant:64).isActive = true
    searchView.heightAnchor.constraint(equalToConstant: 65).isActive = true

    searchController.searchBar.leftAnchor.constraint(equalTo: searchView.leftAnchor).isActive = true
    searchController.searchBar.rightAnchor.constraint(equalTo: searchView.rightAnchor).isActive = true
    searchController.searchBar.topAnchor.constraint(equalTo: searchView.topAnchor,constant:10).isActive = true
    searchController.searchBar.widthAnchor.constraint(equalTo: searchView.widthAnchor).isActive = true
    }
</code></pre>

<p>我也在 <strong>ViewDidLoad()-:</strong></p> 中给出了这一行

<pre><code>self.extendedLayoutIncludesOpaqueBars = true
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我发现 UISearchController 的 searchBar 在设置 translatesAutoresizingMaskIntoConstraints = false 时效果不佳。作为一种解决方法,我将搜索栏嵌入到具有所需约束的占位符 View 中:</p>

<pre><code>let searchBarPlaceholderView = UIView()
searchBarPlaceholderView.addSubview(searchController.searchBar)
searchBarPlaceholderView.translatesAutoresizingMaskIntoConstraints = false
searchBarPlaceholderView.heightAnchor.constraint(equalToConstant: 56).isActive = true
stackView.addArrangedSubview(searchBarPlaceholderView)
</code></pre>

<p>注意,searchController.searchBar 的 translatesAutoresizingMaskIntoConstraints 属性设置为 true。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 搜索栏在点击时隐藏,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45223386/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45223386/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 搜索栏在点击时隐藏