Modify the background of UISearchBar in IOS, and enter the background of the text box and UISearchBar in the search content.

Source: Internet
Author: User

Modify the background of UISearchBar in IOS, and enter the background of the text box and UISearchBar in the search content.

Reprinted please indicate the source: http://blog.csdn.net/android_ls/article/details/39993433

The IOS system version is 6.1.3. The implementation steps are as follows:

1. Add UISearchBar to parent View

_ SearchBar = [[UISearchBar alloc] init]; _ searchBar. frame = CGRectMake (0, 0, self. view. frame. size. width, kSeachBarH); _ searchBar. autoresizingMask = UIViewAutoresizingFlexibleWidth; _ searchBar. delegate = self; _ searchBar. placeholder = @ "Enter name, company name, and company product name"; [self. view addSubview: _ searchBar];
2. Modify the background of the search box
    UIImage *img = [UIImage resizedImage:@"find_bg.png"];    [_searchBar setBackgroundImage:img];
3. Modify the icon on the left of the search input box.

[_ SearchBar setImage: [UIImage resizedImage: @ "ic_search.png"] forSearchBarIcon: UISearchBarIconSearch state: UIControlStateNormal];

4. Modify the background of the search input text

[_ SearchBar setSearchFieldBackgroundImage: [UIImage imageNamed: @ "login_btn_input_side.png"] forState: UIControlStateNormal];

NOTE: For the background of the search input text provided by the designer, if a small square with a rounded corner is provided, we will use the method of stretching the middle part of the image according to common sense, the test results are as follows:


If you ask the designer to re-provide an image with a fixed height (for example, the height is 60), use it as the background for text search, as shown below:


5. Modify the cancel button text color and background image on the right of UISearchBar

# Proxy method of The pragma mark search box. The search input box obtains the focus-(void) searchBarTextDidBeginEditing :( UISearchBar *) searchBar {[searchBar setShowsCancelButton: YES animated: YES]; // modify the cancel button text color and background image on the right of UISearchBar for (UIView * searchbuttons in [searchBar subviews]) {if ([searchbuttons isKindOfClass: [UIButton class]) {UIButton * cancelButton = (UIButton *) searchbuttons; // modify the text color [cancelButton setTitleColor: [UIColor whiteColor] forState: Custom]; [cancelButton setTitleColor: [UIColor whiteColor] forState: progress]; // modify the button background [cancelButton setBackgroundImage: [UIImage resizedImage: @ "progress"] forState: UIControlStateNormal]; [cancelButton setBackgroundImage: nil forState: Success];}
Note: to modify the text color of the cancel button and the code segment of the background image, you must put it in the cancel button to display the modification in the proxy method. Otherwise, the traversal cannot be found.



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.