Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

try using with

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

}

hope this will help you

Try setting text field tint color using UIAppearance

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

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.

UISearchBar in storyboard editor with simulator.


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:

UISearchBar with different colored cursor and cancel button.

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 .

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top