Setting translucent to NO on UISearchBar

后端 未结 2 889
粉色の甜心
粉色の甜心 2021-01-19 02:27

We have a UITableView with a searchbar added with the searchDisplayController.

We want to have translucency off throughout th

2条回答
  •  悲哀的现实
    2021-01-19 03:08

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

提交回复
热议问题