Cursor invisible in UISearchBar iOS 7

后端 未结 6 932
攒了一身酷
攒了一身酷 2020-12-11 15:04

I have UISearchBar in UITableView as a table header. When I push the UISearchBar for start searching, this method is being triggered

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 15:46

    If you want the cursor and cancel button to be different colors...


    Start by setting the view's tint color (not the bar tint) in the storyboard editor, which will be applied to both the cursor and cancel button.


    To make the cursor a different color, you need to do it programatically. The text field is nested a couple levels down in the searchBar's subviews. Use this setTextFieldTintColor helper function to traverse all of the subviews.

    @IBOutlet weak var searchBar: UISearchBar!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // set tint color for all subviews in searchBar that are of type UITextField
        setTextFieldTintColor(to: UIColor.darkText, for: searchBar)
    }    
    
    func setTextFieldTintColor(to color: UIColor, for view: UIView) {
        if view is UITextField {
            view.tintColor = color
        }
        for subview in view.subviews {
            setTextFieldTintColor(to: color, for: subview)
        }
    }
    

    The end result looks like this:

提交回复
热议问题