如何更改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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...