在UISearchBar上将半透明设置为NO

时间:2013-09-25 17:29:53

标签: ios uitableview ios7 uisearchbar uisearchbardisplaycontrol

我们有一个UITableViewsearchbar添加了searchDisplayController

我们希望在整个应用程序中关闭半透明效果。

我对导航栏和其他栏有半透明效果,但在使用显示控制器时没有搜索栏。在应用程序的一部分,当我们使用搜索栏而不是显示控制器时,半透明度设置正确。

如何将显示控制器的UISearchBar的半透明属性设置为NO?

编辑: 这是我在viewDidLoad

中的代码
self.navigationController.navigationBar.translucent = NO;
BOOL t = self.searchDisplayController.searchBar.translucent;
self.searchDisplayController.searchBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
UIBarStyle b1 = self.searchDisplayController.searchBar.barStyle;
UISearchBarStyle b2 = self.searchDisplayController.searchBar.searchBarStyle;
BOOL t2 = self.searchDisplayController.searchBar.translucent;

在调试器中运行,t = YES,t2 = YES。 b1 = UIBarStyleDefault,b2 = UISearchBarStyleDefault。我在错误的位置设置NO吗?我已尝试storyboardviewDidLoad

中的设置

2 个答案:

答案 0 :(得分:6)

对于UISearchBarStyleProminent:

1)务必检查“属性”检查器中搜索栏的“半透明”框。

2)将以下内容添加到viewDidLoad:

self.navigationController.navigationBar.translucent = NO; // If you have a navBar
self.searchDisplayController.searchBar.translucent = NO;

修改来自@RudolfAdamkovic

  

“我发现对于UISearchBarStyleProminent,执行[以下]会有所帮助。这样,您可以在故事板中保留它。”
  searchBar.translucent = YES;
  searchBar.translucent = NO;

对于UISearchBarStyleMinimal:

为了让最小的搜索栏不是半透明的,我已经整理了一个解决方法。

1)务必检查“属性”检查器中搜索栏的“半透明”框。

2)将以下代码添加到viewDidLoad:

self.navigationController.navigationBar.translucent = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor];

3)需要将UIView添加到viewController中。此视图需要为20px高度,并且应与searchBar.barTintColor具有相同的颜色。

注意:我认为需要这种解决方法,因为:"The style UISearchBarStyleMinimal provides no default background color or image but will display one if customized as such."因此,为UISearchBarStyleMinimal获取此功能的唯一方法是设置backgroundColor。

有关详细信息,请参阅UISearchBar documentation

答案 1 :(得分:1)

上述答案都不适用于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];