Hide UISearchBar clear text button

uisearchbar clear button action
uitextfield clear button color
nstextfield clear button
uisearchbar button
swift clear button in textfield
swift uitextfield clear button
uitextfield clear text when editing
swiftui textfield clear button

I would like to know how to hide or not display the UISearchBar cross that appears in the textField fo the UISearchBar

I have tried using this

filterSearchBar.showsCancelButton = NO;

However this is an actual cancel button not the small grey cross, so I would like to know if there is an equivalent for the small grey button that shows in the UISearchBar.

You need to get the textField of the Search Bar:

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;

hope this help! =)

Remove clear button in UISearchBar, Swift. I found a better way that completely removes the button, using clearButtonMode = .never let searchBarStyle = searchBar.value(forKey:  I’d like to hide the cancel button so that the user never sees it. The problem is that the following code hides the button, but only after displaying it to the user for a millisecond (e.g., it flashes on the simulator and device and then disappears out of view). -

There's a better way to do this, and you don't have to use private APIs or traverse subviews to do it, so this solution is App Store safe.

UISearchBar has a built-in API for doing this:

[UISearchBar setImage:forSearchBarIcon:state]

The SearchBar icon key you want is UISearchBarIconClear, and you want the UIControlStateNormal state. Then give it a clear image for the image, and you're done.

So, it should look like this:

[searchBar setImage:clearImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

Remove clear button (grey x) to the right of UISearchBar when , So, my question is this: given that -resignFirstResponder (and -endEditing: , FYI) does not hide the grey x button when a search bar has had text entered into it,  When conforming to UISearchBar delegate we do not get a notification when user clicks the clear text button in the UISearchBar. However, UISearchBar has a subview of type UITextField, and if we conform to UITextFieldDelegate we do get a call when clear text button is clicked in form of: - (BOOL)textFieldShouldClear:(UITextField *)textField

Based on @Gines answer, here is the Swift version:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    guard let firstSubview = searchBar.subviews.first else { return }

    firstSubview.subviews.forEach {
        ($0 as? UITextField)?.clearButtonMode = .never
    }
}

Hide UISearchBar clear text button · GitHub, UISearchBar provides a text field for entering text, a search button, a bookmark button, and a cancel button. A search bar does not actually perform any searches​  Hide UISearchBar clear text button. filterSearchBar.showsCancelButton = NO; However this is an actual cancel button not the small grey cross, so I would like to know if there is an equivalent for the small grey button that shows in the UISearchBar.

Swift 4

Adding to Alexander's answer and block user interaction on clear button:

To hide button:

searchBar.setImage(UIImage(), for: .clear, state: .normal)

To disable user interaction on the clear button, simply subclass UISearchBar

class CustomSearchBar: UISearchBar {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if view is UIButton {
            return view?.superview // this will pass-through all touches that would've been sent to the button
        }
        return view
    }
}

UISearchBar, A clear button, to remove any text from the search field. A Cancel button, to exit the search function. Search bar. Implementing the Search Bar. To  I believe this is a bug more than an enhancement, it prevents the users from dismissing the keyboard when the text is empty. The default behavior for the iOS UISearchbar's ShowsCancelButton property is to display the cancel button as long as the searchbar is highlighted/focused (even if text is empty) and I think the Forms SearchBar should behave the same on iOS I found a thread in the forums

You can remove the clear text button for all UISearchBar instances:

[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].clearButtonMode = UITextFieldViewModeNever;

Search Bars in Xamarin.iOS, On tapping the search bar, the cancel button and the scoped bars It is a subclass of the NSObject and allows representing a query in a rich way with text and icons. Additionally, we can choose to disable the copying and deletion of has no effect if the clear button is present in the UISearchTextField . The UISearchBar class implements a text field control for text-based searches. The control provides a text field for entering text, a search button, a bookmark button, and a cancel button. The UISearchBar object does not actually perform any searches.

What's New in the iOS 13 Search Bar? - Better Programming, When conforming to UISearchBar delegate we do not get a notification when user clicks the clear text button in the UISearchBar. However  class CustomSearchBar: UISearchBar { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let view = super.hitTest(point, with: event) if view is UIButton { return view?.superview // this will pass-through all touches that would've been sent to the button } return view } }

Detecting when clear is clicked in UISearchBar (X button , How to hide the Clear button in Windows Internet Explorer 10. Applies to: Internet Explorer 10. Windows Internet Explorer 10 introduces a new Clear button for text input fields. This button will be displayed in all rendering modes, but only if the text input control has focus and is not empty.

Comments
  • Where do you find the key "_searchField" is it somewhere in documentation?
  • You can find it in the run time headers. I.e. github.com/nst/iOS-Runtime-Headers.
  • While this technically makes the button invisible, you can still tap it. But +1 for using official APIs.
  • anyone found a non-hack solution to prevent interaction to the button?
  • @Tom thanks for the edit. Looks perfect. Is this a correct approach?
  • I actually think Soto_iGhost's approach is best to solve this particular problem. But when I saw your code I couldn't help myself to edit it ☺️ There are of course many approaches to write this same piece of logic, but I like this one the best.
  • This is a good solution for swift 4+, I have been using above methods which takes "_searchField" as key. but application is crashing at iOS 13.2.2. This solution has fixed my problem both for lower iOS versions and latest one.
  • THIS is the only valid non-hackish answer. Good job!
  • This answer looks good - can we still set it for a search bar of a particular view-controller class? I've searchbar at multiple places and need to update this for only one class.
  • this is crashing
  • thanks, it worked!, the key was to search through subviews of first subview of Searchbar. See my answer for swift version
  • some explanation please?