搜索栏在导航栏中更改颜色

Nic*_*nto 5 uinavigationcontroller uisearchbar ios

我在导航项目的标题视图中添加了一个搜索栏.搜索工作正常,但使用导航控制器时,搜索栏的背景会改变颜色,如下所示

我触摸颜色的唯一地方是通过xcode更改故事板中导航项的颜色

将搜索栏放在titleview中的相关代码

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[_searchController.searchBar sizeToFit];
self.navigationItem.titleView = _searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)

导致这种颜色的原因是什么?

在此输入图像描述

试图改变bartint颜色

[_searchController.searchBar setBarTintColor:[UIColor clearColor]];
Run Code Online (Sandbox Code Playgroud)

但我得到了这个 在此输入图像描述

还有一些不起作用的东西

[_searchController.searchBar setBarTintColor:self.navigationController.navigationBar.tintColor];
_searchController.searchBar.backgroundColor = self.navigationController.navigationBar.tintColor;
Run Code Online (Sandbox Code Playgroud)

backgroundColor

似乎这种颜色不同于设置.请看这里的redColor

在此输入图像描述

Dha*_*iya 14

需要设定UISearchBar tint color.

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

现在导航栏看起来像下面的图像:

在此输入图像描述

  • `search.backgroundImage = [UIImage new];`为我修好了; 无需设置`barTintColor`属性. (5认同)