Hide UISearchBar clear text button

无人久伴 提交于 2019-12-03 16:18:15

问题


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.


回答1:


You need to get the textField of the Search Bar:

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

hope this help! =)




回答2:


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



回答3:


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



回答4:


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



回答5:


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

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



回答6:


Swift 3, based on @Alexsander answer:

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



回答7:


Swift 3 solution :

extension UISearchBar{
    var textField : UITextField{
        return self.value(forKey: "_searchField") as! UITextField
    }
}

Usage :

searchBar.textField.clearButtonMode = .never



回答8:


I tried different solutions about this issue, even the one selected in this post, but they didn't work.

This is the way I found to solve this issue:

UIView *subview = [[searchBar subviews] firstObject]; //SearchBar only have one subview (UIView)

//There are three sub subviews (UISearchBarBackground, UINavigationButton, UISearchBarTextField)
for (UIView *subsubview in subview.subviews)
{
    //The UISearchBarTextField class is a UITextField. We can't use UISearchBarTextField directly here.
    if ([subsubview isKindOfClass: [UITextField class]])
    {
            [(UITextField *)subsubview setClearButtonMode:UITextFieldViewModeNever];
    }
}



回答9:


Try this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    UITextField *textField = [searchBar valueForKey:@"_searchField"];
    textField.clearButtonMode = UITextFieldViewModeNever;

}



回答10:


In Case of Swift 2.3 just use :

var searchBar = UISearchBar();
searchBar.frame = CGRectMake(0, 0, 00, 20))
for subview: UIView in (searchBar.subviews.first?.subviews)!
        {
            if (subview.isKindOfClass(UITextField) )
            {
                let textFieldObject = (subview as! UITextField)
                textFieldObject.clearButtonMode = .Never;
            }
        }



回答11:


Swift 2.3, based on @Alexsander answer:

searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted)
searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)



回答12:


Converting Soto_iGhost's answer to Swift 4:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {

    let textField: UITextField = searchBar.value(forKey: "_searchField") as! UITextField
    textField.clearButtonMode = .never
}

If you have an outlet of UISearchBar the you can write above code anywhere in your class.




回答13:


thijsonline's answer in swift:

(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])).clearButtonMode = .never


来源:https://stackoverflow.com/questions/21522104/hide-uisearchbar-clear-text-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!