gpt4 book ai didi

ios - 保留 UISearchBar 的边距

转载 作者:行者123 更新时间:2023-11-28 14:17:00 26 4
gpt4 key购买 nike

我正在尝试创建 UISearchController 并将其搜索栏放入容器 View 中。 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)

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

enter image description here

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

enter image description here

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

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

enter image description here

最佳答案

我最终得到了以下解决方案:在每次呈现和关闭搜索 Controller 后更新搜索栏框架,并在 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
}

关于ios - 保留 UISearchBar 的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com