iOS 设置自带UISearchBar的背景颜色(并改为圆角)

 

 

/** 取消searchBar背景色 */

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

      return image;

}

 

然后在viewdidload里面

/** 设置背景图片 */

    [_searchBar setBackgroundImage:[UIImage imageNamed:@"red_nav_bar"]];   //这里图片的颜色和导航栏一样

/** 设置背景色 */

   [_searchBar setBackgroundColor:[UIColor redColor]];

/** 设置文本框背景 * 

[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"imageView"] forState:UIControlStateNormal];

 

 

    //更改search圆角

        UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

        if (searchField) {

            [searchField setBackgroundColor:[UIColor whiteColor]];

            searchField.layer.cornerRadius = 14.0f;

            searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;

            searchField.layer.borderWidth = 1;

            searchField.layer.masksToBounds = YES;

        }

 

 

posted on   yucaijiang  阅读(13009)  评论(0编辑  收藏  举报

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示