如何自定义UISearchBar的外观

时间:2011-06-01 12:50:18

标签: iphone uisearchbar

我想自定义搜索栏,我的意思是白盒子。我可以做吗? 有关于此的一些文件吗? 有没有办法隐藏白框,但不是字母。 我可以至少让盒子变小吗?我的意思是更少的身高

9 个答案:

答案 0 :(得分:56)

通过IOS 5.0,你有机会使用

  [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];

答案 1 :(得分:28)

以下是我正在寻找的解决方案: 对UISearchBar进行子类化,&覆盖方法layoutSubviews

- (void)layoutSubviews {
   UITextField *searchField;
   NSUInteger numViews = [self.subviews count];
   for(int i = 0; i < numViews; i++) {
      if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.subviews objectAtIndex:i];
      }
   }
   if(!(searchField == nil)) {
       searchField.textColor = [UIColor whiteColor];
       [searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];
       [searchField setBorderStyle:UITextBorderStyleNone];
   }

   [super layoutSubviews];
}

答案 2 :(得分:15)

// Search Bar Customization
// Background Image
[self.searchDisplayController.searchBar setBackgroundImage:[[UIImage alloc]init]];

// Backgroud Color
[self.searchDisplayController.searchBar setBackgroundColor:[UIColor redColor]];

// Search Bar Cancel Button Color
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];

// set Search Bar Search icon
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"search_ico.png"]
                                forSearchBarIcon:UISearchBarIconSearch
                                           state:UIControlStateNormal];

// set Search Bar textfield background image
 [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"]
                                                forState:UIControlStateNormal];

// set Search Bar texfield corder radius
UITextField *txfSearchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
txfSearchField.layer.cornerRadius = 10.8f;

答案 3 :(得分:14)

以下是您可以更改以自定义UISearchBar的一些常用属性: enter image description here

答案 4 :(得分:7)

答案 5 :(得分:1)

很少有尝试包括子类UISearchBar,并且它是layoutSubviewsdrawLayer:。在iOS 5之后,最好的方法是使用UIAppearance。

// Configure your images
UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];

答案 6 :(得分:1)

使用以下代码透明搜索栏

// Set it to your UISearchBar appearance

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage 
                                                forState:UIControlStateNormal];

答案 7 :(得分:1)

  

Swift中的一些例子

RGB

答案 8 :(得分:0)

(UITextField.appearance(whenContainedInInstancesOf:[UISearchBar.self])).defaultTextAttributes = [NSForegroundColorAttributeName:UIColor.white]