On the Uisearchbar of iOS development
Uisearchbar is also one of the most common iOS development controls, click inside to see the properties Barstyle, text, placeholder, and so on. But these attributes are clearly inadequate to meet our development needs. For example: Modify the color of the placeholder, modify the Uisearchbar above the Uitextfield background color, modify Uitextfield above the photos and so on.
In order to achieve the above requirements, it is best to write a uisearchbar subclass called Lssearchbar Bar
LSSearchBar.h as follows:
#import <UIKit/UIKit.h>@interface LSSearchBar : UISearchBar@end
LSSEARCHBAR.M as follows:
#import "LSSearchBar.h" @implementation lssearchbar-(void) layoutsubviews {[Super layoutsubviews]; Find Searchfield Uitextfield *searchfield by traversing self.subviews; Nsuinteger numviews = [self.subviews count]; for (int i = 0; i < numviews; i++) {if ([[[Self.subviews objectatindex:i] Iskindofclass:[uitextfield class]) { Searchfield = [Self.subviews objectatindex:i]; }}//If the above method cannot find Searchfield, then try the following method if (Searchfield = = nil) {Nsarray *arraysub = [self subviews]; UIView *viewself = [Arraysub objectatindex:0]; Nsarray *arrayview = [Viewself subviews]; for (int i = 0; i < Arrayview.count; i++) {if ([[[Arrayview objectatindex:i] Iskindofclass:[uitextfield class] ]) {Searchfield = [Arrayview objectatindex:i]; }}} if (! ( Searchfield = = nil) {//Set color Searchfield.textcolor = [Uicolor Whitecolor]; Set background color [Searchfield setbackground: [UIImage imagenamed:@ "Searchbar"]; [Searchfield Setborderstyle:uitextborderstylenone]; Set the color of the placeholder [Searchfield setvalue:[uicolor Whitecolor] forkeypath:@ "_placeholderlabel.textcolor"]; Set the photo on Searchfield UIImage *image = [UIImage imagenamed:@ "search"]; Uiimageview *iview = [[Uiimageview alloc] initwithimage:image]; Iview.frame = CGRectMake (0, 0, 15, 15); Searchfield.leftview = IView; }} @end
The source of this article just online: http://www.superqq.com/blog/2015/01/19/ioskai-fa-zhi-uisearchbarchu-tan/
On the Uisearchbar of iOS development