In my application, I am adding a UISearchBar.
My intent is to enable the UISearch Bar "X button"(clear button in UITextField) to be always visible.
I have tried using the following code below to try to make the "X Button" be always visible. However, it does not work. If I set tf.clearButtonMode = UITextFieldViewModeNever
, the clear button in uitextfield not showing. I am not sure what is wrong?
I would really appreciate anyone's help here. Why is this not working?
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
I want to always show the clear button if the text length is equal to 0
UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
U can do it in Xib. I am attaching the screenshot.
And programmatically
myUITextField.clearButtonMode = UITextFieldViewModeAlways;
This is the default behavior of the search bar. Because if the UITextField
is blank then there is no need to press it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With