问题
In iOS 11 height of UISearch bar has increased , but I want the same height as in iOS 10. How to do that ? I am using below code to create the 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
}
回答1:
I met the same issue on my app, too. It works perfectly in iOS 10.
However, it will jump out of my view in iOS11 as like this.
Also I don't want to put my search bar into navigationItem.
So maybe you can consider my answer. Here is it.
In your storyboard:
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.
回答2:
//
// 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
}
}
}
}
来源:https://stackoverflow.com/questions/46888872/limit-the-height-of-uisearchbar-in-ios-11