UISearchBar custom

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值