简体   繁体   中英

UISearchbar UI in iOS 12 and 13 are different and not correct

The UI in my searchbar is behaving differently than expected.

I want it to look like this:

在此处输入图片说明

In iOS 13 it looks like this:

在此处输入图片说明

And in iOS 12 it looks like this:

在此处输入图片说明

I am configuring the searchbar in ViewDidLoad with this:

    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])

I've tried to create your searchBar but I didn't test it. You can find answers to your questions from the code snippet. You need to add the UISearchBar extension also to your project to use some methods in the snippet.

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.white
    searchBar.searchTextField.font = UIFont.systemFont(ofSize: 12)
} else {
    searchBar.removeBackgroundImageView()
    searchBar.setTextFieldBackground(color: UIColor.black.withAlphaComponent(0.3))
}

UISearchBar extension

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])
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM