[英]SearchController SearchBar width issue iOS11
I am trying to add SearchController SearchBar programmatically in one of UIVIew
as subView but initially it working properly up to iOS 10 but getting issue in iOS11 width getting increased get displaced. 我试图在
UIVIew
其中一个中以子UIVIew
以编程方式添加SearchController SearchBar,但最初它在iOS 10之前都可以正常运行,但是iOS11宽度出现问题,变得越来越移位。
I tried all possible solutions, Created Custom class init SearchBar frame with CGRect.zero
& even hide cancel button. 我尝试了所有可能的解决方案,使用
CGRect.zero
创建了Custom类init SearchBar框架,甚至隐藏了cancel按钮。 Still i does not understanding where I am doing wrong. 仍然我不明白我在哪里做错了。
Initial Screen: 初始屏幕:
Issue is here when i clicked on SearchBar 当我单击SearchBar时问题就在这里
Irony is its not working even i am trying to reset frame width to 100.0f by calling method viewWillLayoutSubviews
from searchBarShouldBeginEditing
具有讽刺意味的是,即使我试图通过从
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 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 */ }
}
After lot of attempts i resolved issue by following way, 经过多次尝试,我通过以下方式解决了问题,
I removed customised CustomSearchController class & used simple UISearchController
. 我删除了自定义的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
}
And add SearchBarContainerView
class, 并添加
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
}
}
Following is working screenshot: 以下是工作屏幕截图:
Try this code. 试试这个代码。 it worked for me.
它为我工作。 I was also facing the same issue.
我也面临着同样的问题。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchbar.invalidateIntrinsicContentSize()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.