2013-09-25 52 views
4

我们有一个UITableViewsearchbarsearchDisplayController一起添加。在UISearchBar上设置半透明为NO

我们希望在整个应用程序中拥有半透明性。

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

如何将显示控制器的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?香港专业教育学院试图在storyboard这里设置在viewDidLoad

回答

6

对于UISearchBarStyleProminent:

1)一定要选中“半透明”框在属性检查器中的搜索栏。

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

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

编辑@RudolfAdamkovic

“我发现,对于UISearchBarStyleProminent,执行[以下]帮助这样。你可以在Storyboard中保留它。“
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

这没有奏效,这就是为什么我发布的问题。我希望我能接受这个答案是正确的。 –

+0

对,对不起。我只注意到这不是完整的答案。我已经更新了答案,并已验证它适用于我。 –

+0

为什么我的半透明变量设置为YES?设置半透明属性后设置颜色并没有什么不同。我认为我看到它的半透明性......如果我们将导航设置为红色,它下面的搜索栏看起来就会变成粉红色。 –

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