Ios7 set the title attribute of cancelButton on searchbar
[SearchBarsetShowsCancelButton: YES]; after setting, the default value is "cancel ".
Many methods are useless in stackoverflow.
The key here is to get the cancelButton in the searchbar.
Print [searchbar subviews]
The result is as follows:
(Lldb)Po [searchBar subviews]
<__Nsarraym 0xd6b0db0> (
+ H; layer = >
)
That is to say, it only has one subview, which can be obtained from stackoverflow.
The cancelbutton is located at a deeper level.
Print
[[[SearchBar subviews] objectAtIndex: 0] subviews]
Expected result:
(Lldb)Po [[[searchBar subviews] objectAtIndex: 0] subviews]
<__Nsarraym 0xd6b0dd0> (
>,
>,
>
)
That's it.UINavigationButton to set it
The Code is as follows:
SearchBar = [[UISearchBar alloc] initWithFrame: CGRectMake (0, 0,240, 24)]; // searchBar of the navigation bar. delegate = self; [searchBar setShowsCancelButton: YES]; [searchBar setTintColor: [UIColor blackColor]; [searchBar setBarTintColor: [UIColor clearColor]; [searchBar setPlaceholder: @ "Search for series"]; for (UIView * view in [[[searchBar subviews] objectAtIndex: 0] subviews]) {if ([view isKindOfClass: [NSClassFromString (@ "UINavigationButton") class]) {UIButton * cancel = (UIButton *) view; [cancel setTitle: @ "cancel" forState: UIControlStateNormal]; [cancel setTintColor: [UIColor blackColor]; [cancel. titleLabel setTextColor: [UIColor blackColor] ;}}