Remove clear button (grey x) to the right of UISea

2019-01-22 06:54发布

Right, to begin my question, here's some screenies of the problem already solved by the Spotify app:

Spotify's Step 1: Standard UISearchBar not in editing mode.

Step 1 http://i49.tinypic.com/wbtpwi.png

Spotify's Step 2: UISearchBar now in editing mode. Search term entered. Cancel button slides in from the right, and the clear button (grey x) appears.

Step 2 http://i45.tinypic.com/161kbvp.png

Spotify's Step 3: Cancel button pressed; keyboard slides out and the search bar is no longer in editing mode. Search term remains and the grey x button is now hidden.

Step 3 http://i46.tinypic.com/20utv9v.png

At present, the following code fires off when my cancel button is pressed:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

Which results in:

My Step 3: Search bar now not in editing mode. Cancel button and keyboard has slid out. Search term remains but so does the grey x.

Problem http://i46.tinypic.com/rlm4w5.png

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, how does one hide it?

Thanks again, friends.

11条回答
劳资没心,怎么记你
2楼-- · 2019-01-22 07:56

A better way to do this in iOS7 is:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setClearButtonMode:UITextFieldViewModeWhileEditing];

查看更多
The star\"
3楼-- · 2019-01-22 07:57

To expand on Jadariens answer if you never want the grey x to appear you need to use the following

for (UIView *subview in searchBar.subviews)
{
    if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        [(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
    }
}
查看更多
在下西门庆
4楼-- · 2019-01-22 07:59

Looks like iOS 7 changed the view hierarchy of UISearchBar, and the text box is deeper in the view (The above solution didn't work for me). However, modifying the above solution to traverse the whole hierarchy works:

[self configureSearchBarView:[self searchBar]];

- (void)configureSearchBarView:(UIView*)view {
    for (UIView *subview in [view subviews]){
        [self configureSearchBarView:subview];
    }
    if ([view conformsToProtocol:@protocol(UITextInputTraits)]) {
        [(UITextField *)view setClearButtonMode:UITextFieldViewModeWhileEditing];
    }
}
查看更多
淡お忘
5楼-- · 2019-01-22 07:59

For the (x) icon in searchBar. You can use below delegate method.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
}
查看更多
三岁会撩人
6楼-- · 2019-01-22 08:00

Hers is a category I wrote that does this

Category

@implementation UISearchBar (Additions)

- (void)setClearButtonMode:(UITextFieldViewMode)viewMode {
    UITextField *textField = [self findTextFieldInView:self];
    [textField setClearButtonMode:viewMode];
}

- (UITextField *)findTextFieldInView:(UIView *)view {

    for (UIView *subview in view.subviews) {

        if ([subview isKindOfClass:[UITextField class]] ||
            [subview.class isSubclassOfClass:[UITextField class]]) {

            return (UITextField *)subview;
        }

        UITextField *textField = [self findTextFieldInView:subview];

        if (textField) {
            return textField;
        }
    }

    return nil;
}

@end

Usage

[searchBar setClearButtonMode:UITextFieldViewModeWhileEditing];
查看更多
登录 后发表回答