iOS 12和13中的UISearchbar UI不同并且不正确

问题描述 投票:0回答:1

我的搜索栏中的用户界面行为与预期不同。

我希望它看起来像这样:

enter image description here

在iOS 13中看起来像这样:

enter image description here

在iOS 12中看起来像这样:

enter image description here

我正在使用以下方法在ViewDidLoad中配置搜索栏:

    guard let searchField = searchBar.value(forKey: "searchField") as? UITextField else { return }

    searchBar.backgroundColor = .red
    searchField.backgroundColor = UIColor.black.withAlphaComponent(0.3)
    searchField.textColor = .white
    searchField.leftView?.tintColor = .white
    searchField.attributedPlaceholder = NSAttributedString(string: "Search", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
ios swift uisearchbar ios13 ios12
1个回答
0
投票

我曾尝试创建您的searchBar,但未对其进行测试。您可以从代码段中找到问题的答案。您还需要向项目添加UISearchBar扩展名,以使用代码段中的某些方法。

searchBar.tintColor = UIColor.white
searchBar.searchBarStyle = .default
searchBar.placeholder = "Search"
searchBar.setTextFieldPlaceholderColor(color: UIColor.white)
searchBar.backgroundColor = UIColor.red
searchBar.setImage(UIImage(named:"searchIcon"), for: .search, state: .normal)
if #available(iOS 13.0, *) {
    searchBar.searchTextField.backgroundColor = UIColor.black.withAlphaComponent(0.3)
    searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
    searchBar.searchTextField.textColor = UIColor.dolapGreyColor()
    searchBar.searchTextField.font = UIFont.systemFont(ofSize: 12)
} else {
    searchBar.removeBackgroundImageView()
    searchBar.setTextFieldBackground(color: UIColor.black.withAlphaComponent(0.3))
}

UISearchBar扩展名

extension UISearchBar {

    //use for iOS 12 and below
    func removeBackgroundImageView(){
        if let view:UIView = self.subviews.first {
            for curr in view.subviews {
                guard let searchBarBackgroundClass = NSClassFromString("UISearchBarBackground") else {
                    return
                }
                if curr.isKind(of:searchBarBackgroundClass){
                    if let imageView = curr as? UIImageView{
                        imageView.removeFromSuperview()
                        break
                    }
                }
            }
        }
    }

    func setTextFieldBackground(color : UIColor) {
        for subView in self.subviews {
            for subView1 in subView.subviews {
                if subView1.isKind(of: UITextField.self) {
                    subView1.backgroundColor = color
                    (subView1 as? UITextField)?.font = UIFont.systemFont(ofSize: 12)
                }
            }
        }
    }

    func setTextFieldPlaceholderColor(color : UIColor) {
        for subView in self.subviews {
            for subView1 in subView.subviews {
                if subView1.isKind(of: UITextField.self) {
                    if let placeholder = (subView1 as! UITextField).placeholder {
                        (subView1 as! UITextField).attributedPlaceholder = NSAttributedString(string:placeholder,attributes: [NSAttributedString.Key.foregroundColor: color])
                    }
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.