在 iOS 11 中限制 UISearchBar 的高度
Posted
技术标签:
【中文标题】在 iOS 11 中限制 UISearchBar 的高度【英文标题】:Limit the height of UISearchBar in iOS 11 【发布时间】:2017-10-23 12:02:15 【问题描述】:在 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
【问题讨论】:
【参考方案1】:我的应用也遇到了同样的问题。它在 iOS 10 中完美运行。
但是,它会像这样在iOS11中跳出我的视野。
我也不想把我的搜索栏放到导航项中。
所以也许你可以考虑我的回答。就是这样。
在您的故事板中:
代码:
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
现在它就像一个魅力。
【讨论】:
【参考方案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
【讨论】:
以上是关于在 iOS 11 中限制 UISearchBar 的高度的主要内容,如果未能解决你的问题,请参考以下文章
嵌入 UISplitViewController 时,UINavigationBar 中缺少 iOS11 UISearchBar
iOS 11:UISearchController 在 UITableView 和 UISearchBar 之间有一个奇怪的空间