简体   繁体   中英

Change Cancel Button BackgroundColor and Text of UISearchBar in iOS7

I have the following code that changes the background of the Cancel button in the UISearchBar.

for( UIView *subview in self.searchBar.subviews ){
    if ([subview isKindOfClass:[UIButton class]]) {
        [(UIButton *)subview setEnabled:YES];
        [(UIButton *)subview setTitle:@"New Button Text" forState:UIControlStateNormal];
        ((UIButton *)subview).tintColor = [UIColor colorWithRed:4/255.0 green:119/255.0 blue:152/255.0 alpha:1.0];
    }
}

The problem is that this code does not work in iOS7! What changed in the structure of the views in the UISearchBar?

Edit: Tested here and this is the new Hierarchy of the UISearchBar:

UISearchBar:
---UIView:
------UISearchBarBackground
------UISearchBarTextField
------UINavigationButton

The problem is that i cannot test if ([sub isKindOfClass:[UINavigationButton class]]) . This line throws a compile error: Use of undeclared identifier: UINavigationButton

[Edited]

Try this codes.

Add in AppDelegate if you want to change all cancel button.

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                                      [UIColor redColor],
                                                                                                      UITextAttributeTextColor,
                                                                                                      [UIColor whiteColor],
                                                                                                      UITextAttributeTextShadowColor,
                                                                                                      [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                                                      UITextAttributeTextShadowOffset,
                                                                                                      nil]
                                                                                            forState:UIControlStateNormal];

In iOS7 , we need to add objectAtIndex:0 and subviews.

The searchBar sub views hierarchy has been changed in iOS7, try the below:

in iOS7:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];


iOS6 and before:

NSArray *searchBarSubViews =  self.searchBar.subviews;

Solution: I created my own button. This way I can manage all the properties without having to discover what Apple did with the elements

I only have to create a UIView aux to contain the searchBar and the new Button.

self.searchBar.showsCancelButton = NO; //don`t show the original cancelButton

self.cancelSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.cancelSearchButton.frame = CGRectMake(250, 6, 60, 31);
[self.cancelSearchButton setTitle:@"Cancelar" forState:UIControlStateNormal];
[self.cancelSearchButton addTarget:self action:@selector(searchBarCancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
NSString *fontName = [[self.cancelSearchButton titleLabel] font].fontName;
[[self.cancelSearchButton titleLabel] setFont:[UIFont fontWithName:fontName size:11.0]];
UIView *aux = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
aux.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
aux.layer.borderWidth = 1.0f;
aux.layer.borderColor = [UIColor lightGrayColor].CGColor;
aux.layer.cornerRadius = 0.0f;
[aux addSubview:self.searchBar];
[aux addSubview:self.cancelSearchButton];

- (void)searchBarCancelButtonClicked{
    //do whatever I want to do here
}

You can take advantage of the iOS Runtime Property _cancelButton to achieve this.

UIButton *cancelButton = [self.searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];

For changing text

[self.searchBar setValue:@"customString" forKey:@"_cancelButtonText"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM