2016-04-11 153 views
0

我正在設計一個搜索欄。我需要搜索欄在圖像中看起來像一個。嘗試了幾種方式,但沒有改變。非常感謝。 enter image description here自定義搜索欄

[編輯] 使用理查茲代碼後的圖像看起來像這樣。我希望灰色清晰。 enter image description here

+0

什麼是你的搜索欄樣式使用理查茲後碼? –

+0

風格默認@ Dev.RK – iDeveloper

回答

2

self.searchBarIBOutlet。您也可以動態創建它。

self.searchBar.layer.borderWidth = 2.0; 
self.searchBar.layer.borderColor = [UIColor brownColor].CGColor; 
self.searchBar.layer.cornerRadius = 15.0; 
self.searchBar.barTintColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0]; 
self.searchBar.backgroundColor = [UIColor clearColor]; 

UITextField *textField = [self.searchBar valueForKey:@"_searchField"]; 
textField.textColor = [UIColor brownColor]; 
textField.placeholder = @"Search"; 
textField.leftViewMode = UITextFieldViewModeNever; //hiding left view 
textField.backgroundColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0]; 
textField.font = [UIFont systemFontOfSize:18.0]; 
[textField setValue:[UIColor brownColor] forKeyPath:@"_placeholderLabel.textColor"]; 

UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)]; 
imgview.image = [UIImage imageNamed:@"searchIcon.png"]; //you need to set search icon for textfield's righ view 

textField.rightView = imgview; 
textField.rightViewMode = UITextFieldViewModeAlways; 

輸出:

enter image description here

+0

該代碼的作品,但默認搜索欄的灰色仍然存在,因此它看起來不太好。 – iDeveloper

+0

檢查更新的圖像 – iDeveloper

+0

看到輸出..... –

0

你可以用圖像來改變UISearchBar appearance-

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]]; 
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"searchBG.png"]]; 
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal]; 
0

你可以使用一些定製的解決方案(這是定製更好) 例如SSSearchBar或試圖找到一些類似