2012-11-22 27 views

回答

2
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"]; 
    [searchField setBackground:[UIImage imageNamed:@"searchfield_bg"]]; 

    [self.searchBar setTintColor:[UIColor blackColor]]; 
    UIImage *searchimg = [UIImage imageNamed:@"search_panel.png"]; 

    for (UIView *subview in self.searchBar.subviews) { 
     if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
      UIView *bg = [[UIView alloc] initWithFrame:subview.frame]; 
      bg.backgroundColor = [UIColor colorWithPatternImage:searchimg]; 
      [self.searchBar insertSubview:bg aboveSubview:subview]; 
      [subview removeFromSuperview]; 
      [bg release]; 
      break; 
     } 
    } 

進出口使用這些圖片,你可以用你,改變顏色也一樣,我要求製作黑色搜索欄,以便使用黑色

enter image description here enter image description here

,它會看起來像: enter image description here

1

只是setTint顏色這樣的..

[searchBar setBarStyle:UIBarStyleDefault]; 
    [searchBar setTintColor:[UIColor colorWithRed:.81 green:.14 blue:.19 alpha:1]];//set alpha with your requirement 

也與圖像使用該代碼的工作很大..

for (UIView *subview in searchBar.subviews) { 
     if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
      UIView *bg = [[UIView alloc] initWithFrame:subview.frame]; 
      bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top-bar.png"]]; 
      [searchBar insertSubview:bg aboveSubview:subview]; 
      [subview removeFromSuperview]; 
      break; 
     } 
    } 

而對於w與紅色孔搜索欄,然後簡單的複製粘貼此代碼伴侶..在這裏我解決這個問題

for (UIView *subview in searchBar.subviews) { 
     if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
      UIView *bg = [[UIView alloc] initWithFrame:subview.frame]; 
      bg.backgroundColor = [UIColor redColor]; 
      [searchBar insertSubview:bg aboveSubview:subview]; 
      [subview removeFromSuperview]; 
      break; 
     } 
    } 
    [searchBar setTintColor:[UIColor redColor]]; 
+0

我已經試過了,但對我來說只是一個掩飾了粉紅色的色調顯示。 – hanumanDev

+0

然後嘗試用colorWithRed:綠色:藍色隊友想我發佈它.. –

+0

請,如果你有時間:) – hanumanDev

相關問題