2011-06-01 85 views
38

我想自定義搜索欄,我的意思是白盒子。我可以做嗎? 有沒有關於這方面的一些文件? 有沒有辦法隱藏白盒,但不是字母。 我可以至少讓箱子變小嗎?我的意思是少高度如何自定義UISearchBar的外觀

回答

56

通過IOS 5.0,你必須使用

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

我們可以改變它的高度嗎? – 2013-01-01 12:31:59

+0

指定一個背景圖像與你想要的高度,它應該改變它 – 2013-01-01 13:14:04

+0

謝謝@Ilker Baltaci,已經試過它保持在原來的高度 – 2013-01-02 12:26:10

29

這裏的背景是解決方案,我一直在尋找: 子類一個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]; 
} 
+0

@Jhaliya,我應該在哪裏放的這部分代碼以獲取搜索欄自定義背景? – chatur 2012-01-19 09:57:29

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]; 
1

使用下面的代碼,透明的搜索欄

// Set it to your UISearchBar appearance 

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]]; 
[[UISearchBar appearance] setBackgroundImage:backgroundImage]; 
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage 
               forState:UIControlStateNormal]; 
14
// 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; 
8

下面是一些常見的屬性,您可以更改自定義的UISearchBar: enter image description here

0

一些例子Swift

UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()] 
     UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()]) 
     UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor() 

    UISearchBar.appearance().setImage(UIImage(named: "searchBarSearchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal) 
     UISearchBar.appearance().setImage(UIImage(), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)