Limit UISearchBar height in iOS 11

In iOS 11, the height of the UISearch panel has increased, but I want the same height as in iOS 10. How do I do this? I am using the code below to create a 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
}
0
source share
2 answers

I also met the same problem in my application. It works great in iOS 10.

However, it will pop out of my view in iOS11 like this.

enter image description here

Also, I do not want to put my search bar into navigation mode.

So maybe you can consider my answer. There he is.

In the storyboard:

enter image description here

the code:

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

}

Now it works like a charm.

enter image description here

+1
source
//
//  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
            }
        }
    }
}
0
source

Source: https://habr.com/ru/post/1687458/


All Articles