Cursor invisible in UISearchBar iOS 7

2019-01-20 08:00发布

问题:

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

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

for UISearchDisplayController.

But result is like that;

As you can see, there is no cursor, I can start typing and search, everything works fine. Also it's invisible only in iOS 7. However, with iOS 6.1 and iOS 7.1 Beta 3 I could see the cursor. So how can I make UISearchBar cursor visible or how can I add cursor in my UISearchBar?

回答1:

Cursor in the search bar takes color from Search Bar -> View -> Tint Color property. In your case, it is set to White color, so it becomes invisible.



回答2:

try using with

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    self.navigationItem.titleView.tintColor = [UIColor blueColor];

}

hope this will help you



回答3:

Try setting text field tint color using UIAppearance

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor: [UIColor darkGrayColor]];


回答4:

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:



回答5:

Try this it's only one line of code to solve your problem , Change cursor tintColor property white to blue.

  searchBar.tintColor = [UIColor blueColor];

Hope this will help to someone .



回答6:

Your cursor is white because your tintColor property on UISearchBar is set to white.

If you want Cancel btn to be white, but cursor to be black you can use: UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black