gpt4 book ai didi

ios - UISearchController 触摸后看起来不对

转载 作者:行者123 更新时间:2023-11-30 14:03:35 28 4
gpt4 key购买 nike

我以编程方式将UISearchController添加到UIViewController中,当加载UIViewController时一切都很好,但是当我触摸UISearchBar时在UISearchController中,它移动到底部并且看起来不对。我第一次在代码中添加 UI 元素。

    override func viewDidLoad() {
super.viewDidLoad()
self.definesPresentationContext = true
searchController = ({
let controllerSearch = UISearchController(searchResultsController: nil)
controllerSearch.searchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 44)
controllerSearch.delegate = self
controllerSearch.searchBar.delegate = self
controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default
controllerSearch.dimsBackgroundDuringPresentation = false
controllerSearch.hidesNavigationBarDuringPresentation = false
controllerSearch.searchResultsUpdater = self
controllerSearch.searchBar.sizeToFit()
self.view.addSubview(controllerSearch.searchBar)
return controllerSearch
})()
}

虽然我让控制台显示了 UISearchController 的框架,并且它显示了正确的结果(0.0、20.0、320.0、44.0),请帮助我。我还是尝试了一下,但结果是一样的。

  let frame = CGRectMake(0, 20, self.view.frame.size.width, 44)
controllerSearch.searchBar.frame = frame

enter image description here enter image description here

最佳答案

我解决了我的问题。我添加了以下元素

@IBOutlet weak var navigationItemSearch: UINavigationItem!

并更改了我的UISearchController

   searchController = ({
let controllerSearch = UISearchController(searchResultsController: nil)
controllerSearch.delegate = self
controllerSearch.searchBar.delegate = self
controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Prominent
controllerSearch.dimsBackgroundDuringPresentation = false
controllerSearch.hidesNavigationBarDuringPresentation = false // false
controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default
controllerSearch.searchResultsUpdater = self
// placing
controllerSearch.searchBar.sizeToFit()
navigationItemSearch.titleView = controllerSearch.searchBar
//
controllerSearch.searchBar.spellCheckingType = UITextSpellCheckingType.Yes
controllerSearch.searchBar.autocapitalizationType = UITextAutocapitalizationType.None
controllerSearch.searchBar.keyboardType = UIKeyboardType.WebSearch
return controllerSearch
})()

关于ios - UISearchController 触摸后看起来不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32678974/

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