#import <UIKit/UINavigationBar.h>
@implementation MySearchBar
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.tintColor= [UIColor whiteColor];
self.showsCancelButton = YES;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)layoutSubviews
{
[super layoutSubviews];
UITextField *searchField;
UIButton *button;
NSArray *subviewArr = self.subviews;
for(int i = 0; i < subviewArr.count ; i++) {
UIView *viewSub = [subviewArr objectAtIndex:i];
NSArray *arrSub = viewSub.subviews;
for (int j = 0; j < arrSub.count ; j ++) {
id tempId = [arrSub objectAtIndex:j];
if([tempId isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)tempId;
}
if ([tempId isKindOfClass:[UIButton class]]) {
button = (UIButton *)tempId;
}
}
}
//自定义UISearchBar
if(searchField) {
searchField.placeholder = @"输入要查找的关键字";
[searchField setBorderStyle:UITextBorderStyleNone];
//[searchField setBackgroundColor:[UIColor blueColor]];
//[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setTextColor:[UIColor orangeColor]];
[searchField setBackgroundColor:[UIColor clearColor]];
//自己的搜索图标
//NSString *path = [[NSBundle mainBundle] pathForResource:@"search1" ofType:@"png"];
//UIImage *image = [UIImage imageWithContentsOfFile:path];
//UIImageView *iView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search1"]];
//[iView setFrame:CGRectMake(0.0, 0.0, 16.0, 16.0)];
//searchField.leftView = iView;
[searchField setLeftViewMode:UITextFieldViewModeNever];
}
//外部背景
UIView *outView = [[UIView alloc] initWithFrame:self.bounds];
// [outView setBackgroundColor:[UIColor orangeColor]];
UIImage *searchleftImg = [UIImage imageNamed:@"searchbar_left"];
UIImage *searchrightImg = [UIImage imageNamed:@"searchbar_right"];
//UIImage *searchleftImg1 = [searchleftImg stretchableImageWithLeftCapWidth:40 topCapHeight:0];
UIEdgeInsets insets1 = UIEdgeInsetsMake(0, 50, 0, 60);
UIImage *searchleftImg1 = [searchleftImg resizableImageWithCapInsets:insets1];
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 30);
UIImage *searchrightImg1 = [searchrightImg resizableImageWithCapInsets:insets];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(searchField.frame.origin.x-2, searchField.frame.origin.y -8, searchField.frame.size.width, searchField.frame.size.height)];
UIImageView *imageViewleft =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, searchField.frame.size.width/2, searchField.frame.size.height *2)];
imageViewleft.image = searchleftImg1;
UIImageView *imageViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(searchField.frame.size.width/2,0 , searchField.frame.size.width/2, searchField.frame.size.width)];
imageViewRight.image =searchrightImg1;
[view addSubview:imageViewleft];
[view addSubview:imageViewRight];
[self insertSubview:view belowSubview:searchField];
button.titleLabel.text = @"okok";
[button setBackgroundColor:[UIColor clearColor]];
UIButton *button2 = [[UIButton alloc] initWithFrame:button.frame];
button2.titleLabel.text = @"okok";
[self insertSubview:button2 belowSubview:button];
NSLog(@"%@",button);
}
@end
UISearchBar custom
最新推荐文章于 2020-08-26 05:26:32 发布