[英]iOS11 SearchController - SearchBar removal from navigationItem leaves broken UI
[英]SearchController SearchBar width issue iOS11
我試圖在UIVIew
其中一個中以子UIVIew
以編程方式添加SearchController SearchBar,但最初它在iOS 10之前都可以正常運行,但是iOS11寬度出現問題,變得越來越移位。
我嘗試了所有可能的解決方案,使用CGRect.zero
創建了Custom類init SearchBar框架,甚至隱藏了cancel按鈕。 仍然我不明白我在哪里做錯了。
初始屏幕:
當我單擊SearchBar時問題就在這里
具有諷刺意味的是,即使我試圖通過從searchBarShouldBeginEditing
調用viewWillLayoutSubviews
方法將幀寬度重置為100.0f,也無法正常工作
func setUpSearchBar(){
self.automaticallyAdjustsScrollViewInsets = false
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self as GMSAutocompleteResultsViewControllerDelegate
searchController = CustomSearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
//resultsViewController?.view.frame = searchAndButtonView.bounds
//let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: searchView.frame.size.width, height: searchView.frame.size.height)
searchController?.searchBar.delegate = self
searchView.addSubview((searchController?.searchBar)!)
searchView.translatesAutoresizingMaskIntoConstraints = false
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = .minimal
definesPresentationContext = false
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
}
CustomSearchController.swift
class CustomSearchController: UISearchController,UISearchBarDelegate{
let noCancelButtonSearchBar = NoCancelButtonSearchBar()
lazy var _searchBar: NoCancelButtonSearchBar = {
[unowned self] in
let customSearchBar = NoCancelButtonSearchBar(frame: CGRect.zero)
return customSearchBar
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
class NoCancelButtonSearchBar: UISearchBar {
override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) { /*
void */ }
}
經過多次嘗試,我通過以下方式解決了問題,
我刪除了自定義的CustomSearchController類,並使用了簡單的UISearchController
。
func setUpSearchBar(){
self.automaticallyAdjustsScrollViewInsets = false
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self as GMSAutocompleteResultsViewControllerDelegate
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: searchView.frame.size.width, height: searchView.frame.size.height)
let searchBarContainer = SearchBarContainerView(customSearchBar: searchController?.searchBar)
searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)
searchController?.searchBar.delegate = self
searchView.addSubview(searchBarContainer)
searchView.translatesAutoresizingMaskIntoConstraints = false
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = .minimal
definesPresentationContext = false
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
}
並添加SearchBarContainerView
類,
class SearchBarContainerView: UIView {
let searchBar: UISearchBar
init(customSearchBar: UISearchBar) {
searchBar = customSearchBar
super.init(frame: CGRect.zero)
addSubview(searchBar)
}
override convenience init(frame: CGRect) {
self.init(customSearchBar: UISearchBar())
self.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
searchBar.frame = bounds
}
}
以下是工作屏幕截圖:
試試這個代碼。 它為我工作。 我也面臨着同樣的問題。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchbar.invalidateIntrinsicContentSize()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.