我以编程方式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
答案 0 :(得分:0)
我解决了我的问题。我添加了以下元素
@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
})()