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.