2015-01-06 81 views
0

我希望做這樣一個簡單的顏色搜索欄:的UISearchBar背景圖像加工古怪

enter image description here

我這個圖像設置爲背景成功地在我的視圖控制器這樣做圖像:

enter image description here

和與此代碼:

[self.searchBar setTranslucent:NO]; 
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbg.png"] forState:UIControlStateNormal]; 

然而,當我點擊搜索欄,它進入searchdisplaycontroller,它看起來像這樣:

enter image description here

有有一個白色的部分,我無法擺脫掉。我試過這樣做:

[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbg.png"] forState:UIControlStateNormal]; 

但它似乎並沒有工作。我對他們爲什麼採取不同的行爲感到有點困惑,因爲他們都是無人區?

謝謝。

回答

0

嘗試爲UISearchBar圖層設置borderColor和borderWidth它將修復您的顏色變化 請記住,您必須記住,對於ios7和以後的視圖位置是不同的,所以使用我下面提到的。我也有UISearchBar的子類,如果你想你也可以創建一個或你自己的serachBar來代替自己

- (void)setup 
{ 
    self.placeholder  = @"Search Products"; 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { 
     self.backgroundColor = [UIColor lightGrayColor]; 
     UIView *topView = self.subviews[0]; 
     [self searchBarCustomization:topView.subviews]; 
    } else { 
     self.backgroundImage = [UIImage imageWithColor:[UIColor lightGrayColor]]; 
     self.layer.borderColor = [UIColor lightGrayColor].CGColor; 
     self.layer.borderWidth = 0.0; 
     [self searchBarCustomization:self.subviews]; 
    } 
} 

- (void)searchBarCustomization:(NSArray *)subViews 
{ 
    for (UIView *subView in subViews) { 
     if ([subView isKindOfClass:[UITextField class]]) { 
      [self customSearchBarField:subView]; 
     } 
     if ([subView isKindOfClass:[UIButton class]]) { 
      [self customSearchCancelButton:subView]; 
     } 
    } 
} 

- (void)customSearchBarField:(id)searchBarField 
{ 
    UITextField *searchField = (UITextField *) searchBarField; 
    if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { 
     searchField.background = [UIImage imageWithColor:[UIColor whiteColor]]; 
     searchField.borderStyle = UITextBorderStyleNone; 
     searchField.layer.cornerRadius = 5.0; 
    } 
}