在ios7中将UISearchBar的圆角和边框设置为背景(不是UITextField)

时间:2014-04-02 09:27:43

标签: ios objective-c ios7 uisearchbar

如何在ios7中将圆角和边框应用于UISearchBar的背景?我可以改变条形图的背景颜色。

像这样的东西

enter image description here

由于

2 个答案:

答案 0 :(得分:12)

  UISearchBar *searchBar  = [[UISearchBar alloc] init];
  searchBar.layer.cornerRadius  = 6;
  searchBar.clipsToBounds       = YES;
  [searchBar.layer setBorderWidth:1.0];
  [searchBar.layer setBorderColor:[[UIColor lightGrayColor].CGColor];

答案 1 :(得分:0)

要使搜索栏成为圆形,请使用:

for (UIView *searchBarSubview in [mySearchBar subviews]) {
    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
        @try {

            [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
        }
        @catch (NSException * e) {
            // ignore exception
        }
    }
}

要更改背景颜色,请使用:

yourSearchBar.barTintColor = [UIColor redColor]; // Use you necessary color.