【问题标题】:Preserve margins for UISearchBar为 UISearchBar 保留边距
【发布时间】:2018-09-02 21:18:09
【问题描述】:

我正在尝试创建 UISearchController 并将其搜索栏放入容器视图中。 searchView 是出口。这是我的代码:

    let searchController = UISearchController(searchResultsController: nil)
    self.searchController = searchController

    let searchBarBackground = UIImage.roundedImage(UIImage.image(with: #colorLiteral(red: 0.5568627451, green: 0.5568627451, blue: 0.5764705882, alpha: 0.2), size: size), cornerRadius: cornerRadius)
    searchController.searchBar.setSearchFieldBackgroundImage(searchBarBackground, for: .normal)
    searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(8.0, 0.0)
    searchController.searchBar.searchBarStyle = .minimal
    searchController.searchBar.barStyle = .black
    searchController.searchBar.delegate = self

    searchView.addSubview(searchController.searchBar)

我不知道如何让搜索栏保留主视图的边距。当我尝试向容器视图添加约束并将搜索栏添加为子视图时,搜索栏不遵守尾随约束。

即使手动设置搜索栏框架等于viewDidLayoutSubviews 中的内容视图边界也无济于事。仅当我将前导和尾随约束设置为 0 而不考虑超级视图边距时,它看起来才正常。但它的边距只有 8pt,我需要更多。

我怎样才能实现它?也许可以更改搜索栏的默认边距?谢谢。

如果需要,来自 IB 的屏幕截图(它只是对值 -8 的边距进行限制以补偿搜索栏内部边距):

【问题讨论】:

  • 你能展示你尝试过的约束吗?
  • @RakeshaShastri 已添加。
  • 为什么前导和尾随常量都是负数?前导不应该是正数,尾随是负数吗?
  • 不,约束有效。内容视图需要框架,但在iOS 11的UISearchController中使用时,搜索栏只是拉伸到屏幕的所有宽度。在较低版本中没有这样的问题。

标签: ios swift uikit uisearchbar uisearchcontroller


【解决方案1】:

我最终得到了以下解决方案:在每次呈现和关闭搜索控制器后更新搜索栏框架,并在 viewDidAppear(_:) 中进行初始设置。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    searchController?.searchBar.frame.size.width = searchView.bounds.width
}

func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.frame.size.width = self.searchView.bounds.width
}

func didDismissSearchController(_ searchController: UISearchController) {
    searchController.searchBar.frame.size.width = self.searchView.bounds.width
}

【讨论】:

    【解决方案2】:

    问题在于您以编程方式创建视图并使用自动调整大小掩码。

    https://developer.apple.com/documentation/uikit/uiview/1622572-translatesautoresizingmaskintoco

    默认情况下,对于您以编程方式创建的任何视图,该属性都设置为 true。

    简单的 UISearchBar 示例:

    class ViewController: UIViewController, UISearchBarDelegate {
    
        weak var searchBar: UISearchBar?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let searchBar = UISearchBar(frame: .zero)
            self.searchBar = searchBar
            searchBar.searchBarStyle = .minimal
            searchBar.barStyle = .black
            searchBar.delegate = self
            searchBar.translatesAutoresizingMaskIntoConstraints = false
    
            view.addSubview(searchBar)
            NSLayoutConstraint.activate([
                searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 50.0),
                searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40.0),
                view.trailingAnchor.constraint(equalTo: searchBar.trailingAnchor, constant: 40.0),
            searchBar.heightAnchor.constraint(equalToConstant: 36.0)
                ])
        }
    }
    

    【讨论】:

    • 它适用于常规搜索栏,但我想使用搜索控制器的默认行为(隐藏导航栏、暗背景等)而不自己实现它。并且控制器的搜索栏不适用于自动布局约束(它只是在编辑开始时消失)。当我将搜索栏作为子视图添加到内容视图时它可以工作,但在这种情况下,搜索栏在 iOS 11 中的宽度无效。
    • 这是自 2014 年以来的已知问题,这里是开放的radar
    猜你喜欢
    • 2017-01-02
    • 2015-10-12
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    相关资源
    最近更新 更多