2015-03-02 75 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