【问题标题】:searchbar changes color when in navigation bar搜索栏在导航栏中时更改颜色
【发布时间】:2015-05-02 14:05:54
【问题描述】:

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

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

将搜索栏放入titleview的相关代码

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

是什么导致了这种颜色?

尝试用

改变 bartint 颜色
[_searchController.searchBar setBarTintColor:[UIColor clearColor]];

但我收到了这个

还有一些不工作的东西是

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

backgroundColor

似乎这种颜色与设置不同。用 redColor 看这里

【问题讨论】:

  • 我试过clearColor,不行
  • 如何制作像你上传的问题图片一样的gif图片?
  • 我在 os x 上使用 LICEcap

标签: ios uinavigationcontroller uisearchbar


【解决方案1】:

需要设置UISearchBartint color

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

现在导航栏如下图所示:

【讨论】:

  • 不幸的是,它不起作用..我添加了 gif 以了解它与编辑中的代码的行为方式
  • search.backgroundImage=[UIImage new]; 为我修复了它;无需设置barTintColor 属性。
【解决方案2】:

对于 Swift 用户,只需添加这两行:-

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

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多