Ios7 set the title attribute of cancelButton on searchbar

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.