2015-03-02 112 views
5

我在navigationitem的titleview中添加了一個搜索欄。搜索工作正常,但使用導航控制器時,搜索欄的背景改變顏色,看到下面搜索欄在導航欄中改變顏色

,我淡淡的色彩是通過Xcode的改變導航項目的顏色在故事板唯一的地方

爲把搜索欄在titleview的

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
[_searchController.searchBar sizeToFit]; 
self.navigationItem.titleView = _searchController.searchBar; 

什麼導致了這種着色相關的代碼?

enter image description here

試圖與

[_searchController.searchBar setBarTintColor:[UIColor clearColor]]; 

改變bartint顏色,但我得到的是不工作這enter image description here

也事

[_searchController.searchBar setBarTintColor:self.navigationController.navigationBar.tintColor]; 
_searchController.searchBar.backgroundColor = self.navigationController.navigationBar.tintColor; 

,並與backgroundColor

看來,這種顏色是某種方式以外的設置。看到這裏有redColor

enter image description here

+0

我試過clearColor,不工作 – 2015-03-02 11:34:38

+0

如何讓你的gif圖像像上傳的問題圖像? – 2015-05-02 12:22:17

+0

我使用LICEcap的操作系統x – 2015-05-02 18:26:18

回答

14

需要設置UISearchBartint color

UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, 100, 20)]; 
[search setBarTintColor:[UIColor clearColor]]; 
search.backgroundImage=[UIImage new]; 
self.navigationItem.titleView=search; 

現在導航欄看起來像下面的圖片:

enter image description here

+0

不幸的是,它不工作..我添加了gif,它如何與代碼在編輯 – 2015-03-02 11:21:38

+0

真棒!謝謝十億!你能解釋爲什麼這個工作嗎? – 2015-03-05 15:45:56

+5

'search.backgroundImage = [UIImage new];'爲我修好了;不需要設置barTintColor屬性。 – 2016-02-08 21:44:06

4

對於SWIFT用戶,只需添加以下兩行: -

searchBar.barTintColor = UIColor.clearColor() 
searchBar.backgroundImage = UIImage() 
self.navigationItem.titleView = searchBar