MySearchBar.h is as follows:
@interface Mysearchbar:uisearchbar
-(void) layoutsubviews;
@end
MYSEARCHBAR.M is as follows:
#import "MySearchBar.h"
@implementation Mysearchbar
-(void) Layoutsubviews {
Uitextfield *searchfield;
Nsuinteger numviews = [self.subviews count];
for (int i = 0; i < numviews; i++) {
if ([[[Self.subviews objectatindex:i] Iskindofclass:[uitextfield class]]) {//conform?
Searchfield = [Self.subviews objectatindex:i];
}
}
if (!) ( Searchfield = = nil)) {
Searchfield.textcolor = [Uicolor Redcolor];
[Searchfield Setborderstyle:uitextborderstyleroundedrect];
UIImage *image = [uiimage imagenamed: @ "departure location. png"];
Uiimageview *iview = [[Uiimageview alloc] initwithimage:image];
Searchfield.leftview = IView;
}
[Super Layoutsubviews];
}
@end
Application:
Mysearchbar *searchbar = [Mysearchbar alloc] Initwithrct:cgrectmark (10, 10, 100,30)];
Searchbar.backgroundcolor = [Uicolor Clearcolor]; Modify the search box background
Searchbar.showsscopebar = NO;
[[Searchbar.subviews Objectatindex:0]removefromsuperview]; Remove the background of the search box
For (UIView *subview in searchbar.subviews) {
if ([Subview iskindofclass:nsclassfromstring (@ "Uisearchbarbackground")]) {
[Subview Removefromsuperview];
Break
}
}
UIView *baview = [[UIView alloc] init];
Baview.backgroundcolor = [Uicolor Clearcolor];
//
[Searchbar Insertsubview:baview atindex:1];