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

后端 未结 3 1579
[愿得一人]
[愿得一人] 2020-12-29 16:51

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

But

相关标签:
3条回答
  • 2020-12-29 17:24

    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:

    0 讨论(0)
  • 2020-12-29 17:33

    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];
    
    0 讨论(0)
  • 2020-12-29 17:34

    Try this:

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

    Hopefully this will help you.

    0 讨论(0)
提交回复
热议问题