在iOS 11中限制UISearchBar的高度

时间:2017-10-23 12:02:16

标签: uisearchbar ios11

在iOS 11中,UISearch栏的高度增加了,但我希望与iOS 10中的高度相同。如何做到这一点?我使用下面的代码来创建searchController。

    searchController = UIUtils.searchControllerInitialize(self)
    searchController.searchResultsUpdater = self
    searchController.delegate = self
    searchController.searchBar.delegate = self

    viewTemp = UIView(frame: CGRect(x: 0.0, y: 64.0,width: UIScreen.main.bounds.size.width , height: 44))
    viewTemp.addSubview(self.searchController.searchBar)
    self.view.addSubview(viewTemp);


    class func searchControllerInitialize(_ forViewController: UIViewController) -> UISearchController  {
    let controller = UISearchController(searchResultsController: nil)
    controller.hidesNavigationBarDuringPresentation = true
    // This property dismiss the background the navigation bar
    controller.dimsBackgroundDuringPresentation = false
    controller.definesPresentationContext = true
    forViewController.definesPresentationContext = true
    controller.searchBar.sizeToFit()

    let topView: UIView = controller.searchBar.subviews[0] as UIView
    for subView in topView.subviews {
        if subView.isKind(of: NSClassFromString("UITextField")!) {
            (subView as! UITextField).returnKeyType = UIReturnKeyType.search
            (subView as! UITextField).enablesReturnKeyAutomatically = true
        }
    }

    let viewS = UIView(frame: CGRect(x: 0.0, y: 0.0,width: UIScreen.main.bounds.size.width , height: 64))
    viewS.backgroundColor = UIColor.DTColor()
    controller.view.addSubview(viewS)
    controller.hidesNavigationBarDuringPresentation = false

    if #available(iOS 11.0, *) {
        controller.searchBar.translatesAutoresizingMaskIntoConstraints = false
        controller.searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
    }

    return controller
}

2 个答案:

答案 0 :(得分:1)

我也在我的应用上遇到了同样的问题。它在iOS 10中完美运行。

但是,它会像我一样跳出iOS11中的视图。

enter image description here

此外,我不想将搜索栏放入navigationItem。

所以也许你可以考虑我的答案。就是这样。

在你的故事板中:

enter image description here

代码:

override func viewDidLoad() {

    super.viewDidLoad()

    self.setupSearchController()

}

func setupSearchController() {

    self.searchResultController = UISearchController(searchResultsController: nil)

    self.searchResultController.searchResultsUpdater = self

    self.searchResultController.delegate = self

    self.searchResultController.hidesNavigationBarDuringPresentation = false

    self.searchResultController.dimsBackgroundDuringPresentation = false

    self.searchResultController.searchBar.delegate = self

    self.searchResultController.searchBar.searchBarStyle = .minimal

    self.searchResultController.searchBar.tintColor = UIColor.white

    self.phoneSearchView.searchBarContainer.addSubview(self.searchResultController.searchBar)

    self.memberListTableView.tableHeaderView = self.phoneSearchView

}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    self.searchResultController.searchBar.sizeToFit()

    self.searchResultController.searchBar.frame.size.width = self.phoneSearchView.searchBarContainer.frame.size.width

    self.searchResultController.searchBar.frame.size.height = self.phoneSearchView.searchBarContainer.frame.size.height

}

现在它就像一个魅力。

enter image description here

答案 1 :(得分:0)

//
//  Created by Sang Nguyen on 10/23/17.
//  Copyright © 2017 Sang. All rights reserved.
//

import Foundation
import UIKit

class CustomSearchBarView: UISearchBar {
    final let SearchBarHeight: CGFloat = 44
    final let SearchBarPaddingTop: CGFloat = 8
    override open func awakeFromNib() {
        super.awakeFromNib()
        self.setupUI()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
       // fatalError("init(coder:) has not been implemented")
    }
    func findTextfield()-> UITextField?{
        for view in self.subviews {
            if view is UITextField {
                return view as? UITextField
            } else {
                for textfield in view.subviews {
                    if textfield is UITextField {
                        return textfield as? UITextField
                    }
                }
            }
        }
        return nil;
    }
    func setupUI(){
        if #available(iOS 11.0, *) {
            self.translatesAutoresizingMaskIntoConstraints = false
            self.heightAnchor.constraint(equalToConstant: SearchBarHeight).isActive = true
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        if #available(iOS 11.0, *) {
            if let textfield = self.findTextfield() {
                textfield.frame = CGRect(x: textfield.frame.origin.x, y: SearchBarPaddingTop, width: textfield.frame.width, height: SearchBarHeight - SearchBarPaddingTop * 2)`enter code here`
                return
            }
        }
    }
}