iOS7.1中的颜色变化,如何更改搜索栏颜色?

时间:2014-03-11 04:15:19

标签: ios ios7 uisearchbar uitabbar

在iOS7.0.3 - 7.0.6上,我的searchBar颜色为金色/黄色,如下所示: enter image description here

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

enter image description here

我设置

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

我尝试了很多方法,但都失败了。任何人都可以找出iOS 7.1中的变化吗?

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

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

我需要指出黄金状态栏是searchBar的子视图,它的框架是CGRectMake(0, -20, 320, 20),它的背景颜色是金色。

首先,我设置了这个:

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

看起来像这样:

enter image description here

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

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

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

最后,searchBar与iOS 7.0上的情况完全相同

enter image description here

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

3 个答案:

答案 0 :(得分:4)

试试这个:

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

希望这会对你有帮助。

答案 1 :(得分: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];

答案 2 :(得分:0)

我使用next方法更改searchBar的backgroundColor

1.由@Ben Jackson建议 - 设置BackgroundImage

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

2.根据设计将textField改为你需要的

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

结果:

enter image description here

enter image description here

相关问题