UISearchBar的使用

    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.barTintColor = BLUE_COLOR;
1.0 更改背景色 / 添加边框 / 
    UIView *searView = [[self.searchBar.subviews firstObject].subviews lastObject];
    searView.backgroundColor = BLUE_COLOR;
    self.searchBar.layer.cornerRadius = 10;
    self.searchBar.layer.masksToBounds = YES;
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 255.0/255.0, 255.0/255.0, 255.0/255.0, 1 });
    CGColorSpaceRelease(colorSpace2);
    [self.searchBar.layer setBorderColor:colorref2];//边框颜色
    [self.searchBar.layer setBorderWidth:0.5];//边框宽度
    CGColorRelease(colorref2);
    self.searchBar.placeholder = @"查找公司";
2.0 遍历更换 放大镜 和 占位文字颜色
    for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
       
        if ([subview isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField*)subview;
           
//            textField.textColor = [UIColor redColor];                         //修改输入字体的颜色
//            [textField setBackgroundColor:[UIColor grayColor]];      //修改输入框的颜色
            [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];   //修改placeholder的颜色
            UIImage *image = [UIImage imageNamed:@"minge_edit"];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            [textField setLeftView:imageView];
        } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subview removeFromSuperview];
        }
    }
    [self addSubview:self.searchBar];

posted on 2016-04-05 10:54  杨德明  阅读(218)  评论(0编辑  收藏  举报

导航