如何解决如何更改UISearchBar的高度?
问题:尽管能够修改其他一些属性,例如文本颜色和字体,图像颜色,占位符文本等,但我无法更改UISearchBar的高度或拐角半径。
长话短说,我已经尝试了所有您可能想到的建议。
您可以在Stackoverflow或Google上找到有关此问题的所有信息-我已经看到了。
这是我当前的代码:
UISearchBar
import UIKit
class SearchBar: UISearchBar {
let placeholderText = "Search..."
let font: UIFont = .font(type: .helveticaNeueRegular,size: 12)
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
// Not working
setFrame(frame: CGRect(x: 0.0,y: 0.0,width: frame.size.width - 16.0,height: 100))
setHeight()
setCornerRadius()
// Working
setPlaceholderText(string: self.placeholderText,font: self.font)
setTextFieldColor(color: .headerBackground)
setSearchImageColor(color: .black)
setPositionAdjustment(UIOffset(horizontal: 12,vertical: 0),for: .search)
searchTextPositionAdjustment = UIOffset(horizontal: 4,vertical: 0)
backgroundImage = UIImage()
tintColor = .lightBlue
barTintColor = .white
super.draw(rect)
}
}
UISearchController
import UIKit
class SearchController: UISearchController {
private lazy var customSearchBar = SearchBar()
override var searchBar: UISearchBar { customSearchBar }
override init(searchResultsController: UIViewController!) {
super.init(searchResultsController: searchResultsController)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
hidesNavigationBarDuringPresentation = false
dimsBackgroundDuringPresentation = false
}
}
SearchView (包含SearchController)
import UIKit
final class SearchView: NSObject,UISearchResultsUpdating,UISearchBarDelegate {
// MARK: Private Properties
private var request: URLSessionTask?
private let throttler = Throttler(delay: 1)
private var prevIoUsQuery = ""
let searchController: SearchController
let searchResultsController = SearchResultsTableViewController()
private lazy var loadingViewController = LoadingViewController()
required override init() {
searchController = SearchController(searchResultsController: searchResultsController)
super.init()
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
}
// MARK: - UISearchBarDelegate
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {...}
func updateSearchResults(for searchController: UISearchController) {...}
}
此SearchView在 TableViewController 中实例化,并添加为标题:
tableView.tableHeaderView = searchView.searchController.searchBar
searchView.searchResultsController.delegate = self
以下是用于从UISearchBar检索UITextField的方法:
import UIKit
extension UISearchBar {
private func getViewElement<T>(type: T.Type) -> T? {
let theView = subviews.flatMap({ $0.subviews })
let theSubviews = theView.flatMap({ $0.subviews })
guard let element = theSubviews.filter({ $0 is T }).first as? T else { return nil }
return element
}
func getSearchBarTextField() -> UITextField? {
return getViewElement(type: UITextField.self)
}
func setFont(font: UIFont) {
if let textField = getSearchBarTextField() {
textField.font = font
}
}
func setPlaceholderText(string: String,font: UIFont) {
if let textField = getSearchBarTextField() {
textField.placeholder = string
textField.font = font
}
}
func setPlaceholderTextColor(color: UIColor) {
if let textField = getSearchBarTextField() {
textField.attributedplaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "",attributes: [NSAttributedString.Key.foregroundColor: color])
}
}
func setTextColor(color: UIColor) {
if let textField = getSearchBarTextField() {
textField.textColor = color
}
}
func setTextFieldColor(color: UIColor) {
if let background = getSearchBarTextField()?.subviews.first {
background.backgroundColor = color
}
}
func setClearButtonColor(color: UIColor) {
if let textField = getSearchBarTextField() {
// swiftlint:disable force_cast
let button = textField.value(forKey: "clearButton") as! UIButton
if let image = button.imageView?.image {
button.setimage(image.transform(withNewColor: color),for: .normal)
}
}
}
func setSearchImageColor(color: UIColor) {
if let imageView = getSearchBarTextField()?.leftView as? UIImageView {
imageView.tintColor = color
}
}
func setFrame(frame: CGRect?) {
if let background = getSearchBarTextField() {
background.bounds = frame ?? CGRect(x: 0,y: 0,width: background.frame.size.width,height: 100)
}
}
func setCornerRadius(radius: CGFloat = 0) {
if let background = getSearchBarTextField() {
background.layer.cornerRadius = radius
background.layer.masksToBounds = true
}
}
func setHeight(height: CGFloat = 100) {
if let background = getSearchBarTextField()?.subviews.first {
background.bounds.size.height = height
}
}
}
func setFont(font: UIFont) {... }
func setPlaceholderText(string: String,font: UIFont) {...}
func setPlaceholderTextColor(color: UIColor) {...}
func setTextColor(color: UIColor) {...}
func setTextFieldColor(color: UIColor) {...}
func setSearchImageColor(color: UIColor) {...}
func setFrame(frame: CGRect?) {...}
func setCornerRadius(radius: CGFloat = 0) {...}
func setHeight(height: CGFloat = 100) {...}
问题显然不在于可以访问UITextField对象。我可以验证是否在调试器中获得了视图,并且进行了一些修改。
似乎框架已被其他地方覆盖?但这不能解释拐角半径...
我尝试修改UISearchBar中的其他子视图,甚至UITextField的子视图,但没有任何效果。
最近有没有人遇到这些问题并找到了解决方案?
Xcode 11,Swift 5。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。