Colour changed in iOS7.1, how to change searchBar colour?

て烟熏妆下的殇ゞ 提交于 2019-11-29 04:05:14

问题


On iOS7.0.3 - 7.0.6, my searchBar colour is Gold/yellow colour like this:

But on iOS 7.1, colour becomes like this:

I set

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

I've tried so many ways and all are failed. Can anyone figure out what changes in iOS 7.1?

============== My fix ===============

I fix this problem by covering a view on searchBar and add the search text filed as subview on this new view.

I need point out that the gold status bar is a subView of searchBar, and it's frame is CGRectMake(0, -20, 320, 20) and it's background colour is gold.

At first, I set this:

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

and looks like this:

Then, I expand the view cover the status bar, I changed the view's frame.size.height + searchBar's height, then use this line:

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

to get the textSearchField, then add this textSearchField to the cover view.

At last, the searchBar is exactly like when on iOS 7.0

Not a good way, I need figure out what changes on iOS 7.1 and use a right way to implement this.


回答1:


Try this:

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

Hopefully this will help you.




回答2:


None of the above answers worked for me on iOS 7/8. Here's some setup code that did the trick:

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];



回答3:


I used next approach for changing backgroundColor of searchBar

1.As suggested by @Ben Jackson - set BackgroundImage

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

2.Change textField to what u need according to design

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;
    }
}

Also method for image from color - here

Result:



来源:https://stackoverflow.com/questions/22316346/colour-changed-in-ios7-1-how-to-change-searchbar-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!