【问题标题】:Colour changed in iOS7.1, how to change searchBar colour?iOS7.1颜色变化,如何改变搜索栏颜色?
【发布时间】:2014-04-14 12:16:12
【问题描述】:

在 iOS7.0.3 - 7.0.6 上,我的 searchBar 颜色是金色/黄色,如下所示:

但在 iOS 7.1 上,颜色变成了这样:

我设置了

searchBar.tintColor = [UIColor clearColor];
searchBar.backgroundColor = goldColor;
searchBar.tintColor = [UIColor blackColor];

我尝试了很多方法,但都失败了。谁能弄清楚 iOS 7.1 有哪些变化?

============== 我的修复================

我通过覆盖 searchBar 上的一个视图并将搜索文本作为子视图添加到这个新视图上来解决这个问题。

需要指出的是,金色状态栏是searchBar的一个子View,它的frame是CGRectMake(0, -20, 320, 20),它的背景颜色是金色。

一开始我是这样设置的:

_searchBar.translucent = YES;
_searchBar.scopeBarBackgroundImage = [self imageWithColor:UWGold];

看起来像这样:

然后,我将视图展开覆盖状态栏,我更改了视图的 frame.size.height + searchBar 的高度,然后使用此行:

UITextField *textSearchField = [_searchBar valueForKey:@"_searchField"];

获取 textSearchField,然后将此 textSearchField 添加到封面视图。

最后,searchBar 和 iOS 7.0 上的完全一样

这不是一个好方法,我需要弄清楚 iOS 7.1 上的哪些变化并使用正确的方法来实现。

【问题讨论】:

  • 这里有同样的问题。也在等待一个好的解决方法。也许这是一个错误?
  • 我不知道...我的 tabBar 颜色在 7.0 和 7.1 上也不同。也许这与 7.1 新引入的辅助功能中的“深色”有关?
  • 即使是这样,只要在我的情况下将其关闭,它也不应该影响应用程序。我仍然认为这是一个错误,你会向 Apple 报告吗?
  • 小心[_searchBar valueForKey:@"_searchField"]。它正在访问一个私有实例变量,Apple 可能不喜欢。

标签: ios ios7 uisearchbar uitabbar


【解决方案1】:

试试这个:

if(IOS_7)
{
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
}

希望这会对你有所帮助。

【讨论】:

  • 感谢您的回答,但是,事情并没有如我所愿,如果我使用 UISearchBarStyleMinimal,那么我的 searchBar 的颜色将变为金色,但是,我想在未点击 searchBar 时保持灰色样式。另外,如果我使用 backgroundImage,范围栏和搜索栏是分开的。我不知道栏的颜色发生了什么变化。
  • 另外,在iOS7.1上,我使用的进度Hud,tabbar'c颜色和其他一些颜色出现错误,searchBar是我最后一个还没修复的东西......
  • Irfan,我用一种不正常的方式修复了它,尽管我认为这不是修复它的好方法。我的答案将在我的问题下方
【解决方案2】:

以上答案均不适用于 iOS 7/8。这里有一些设置代码可以解决问题:

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
searchBar.scopeButtonTitles = @[@"Scope1", @"Scope2"];
searchBar.selectedScopeButtonIndex = 0;
searchBar.backgroundColor = [UIColor clearColor];
searchBar.barTintColor = [UIColor clearColor];
searchBar.translucent = YES; // SUPER IMPORTANT, REMOVING THIS MESSED UP THE SCOPE BAR

// ONLY USE IMAGES, NOT BACKGROUND COLORS
UIImage *searchBarBackgroundImage = [[UIImage imageNamed:@"SearchBarBackgroundImage"];
UIImage *scopeBarBackgroundImage = [[UIImage imageNamed:@"ScopeBarBackgroundImage"];
[searchBar setBackgroundImage:searchBarBackgroundImage
               forBarPosition:UIBarPositionAny
                   barMetrics:UIBarMetricsDefault];
searchBar.scopeBarBackgroundImage = scopeBarBackgroundImage;
searchBar.tintColor = [UIColor whiteColor];

【讨论】:

  • 此代码不允许更改 UITextField 的文本颜色。你解决了吗?
【解决方案3】:

我使用了下一种方法来改变 searchBar 的 backgroundColor

1.正如@Ben Jackson 所建议的 - 设置 BackgroundImage

[self.searchBar setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forBarPosition:UIBarPositionAny
                        barMetrics:UIBarMetricsDefault];

2.根据设计将文本字段更改为您需要的内容

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for( int i = 0; i < searchBarSubViews.count; i++) {
    if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
        UITextField *searchTextField = (UITextField *)[searchBarSubViews objectAtIndex:i];
        [searchTextField setTintColor:[UIColor blueColor]];
        searchTextField.placeholder = @"Search";
        searchTextField.backgroundColor = [UIColor whiteColor];
        searchTextField.layer.borderColor = [UIColor blueColor].CGColor;
        searchTextField.layer.borderWidth = 1.0f;
        searchTextField.layer.cornerRadius = 8.0f;
        searchTextField.leftViewMode = UITextFieldViewModeNever;
    }
}

还有从颜色获取图像的方法 - here

结果:

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多