SearchController SearchBar宽度问题iOS11

时间:2017-11-09 09:22:02

标签: ios swift ios11 swift4 uisearchcontroller

我正在尝试以UIVIew之一编程方式将SearchController SearchBar添加为subView,但最初它可以正常运行到iOS 10,但是在iOS11宽度上的问题变得越来越大。

我尝试了所有可能的解决方案,使用CGRect.zero创建自定义类init SearchBar框架&甚至隐藏取消按钮。我还是不明白我做错了什么。

初始屏幕:

enter image description here

当我点击SearchBar

时,问题就在这里

enter image description here

通过从viewWillLayoutSubviews

调用方法searchBarShouldBeginEditing,即使我尝试将帧宽重置为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 */ }

}

2 个答案:

答案 0 :(得分:1)

经过多次尝试后,我按照以下方式解决了问题,

我删除了自定义的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  
    }  
}

以下是工作截图:

enter image description here

答案 1 :(得分:0)

试试这段代码。它对我有用。我也面临同样的问题。

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        searchbar.invalidateIntrinsicContentSize()
    }