我无法成功地将UINavigationBar的barTintColor设置为clearColor

时间:2022-03-07 21:12:51

There's a strange iOS 7 thing about UINavigationBar. If you try to set it's barTintColor property to [UIColor clearColor] or any color with a alpha = 0, it totally ignores that value. So for instance, if you write:

UINavigationBar有一个奇怪的ios7功能。如果您试图将它的barTintColor属性设置为[UIColor clearColor]或任何alpha = 0的颜色,它将完全忽略该值。例如,如果你写:

[[navigationBar setBarTintColor:[UIColor clearColor]];

It doesn't respect the "clear" part at all. Same result with colorWithRed:green:blue:alpha.

它根本不尊重“清晰”的部分。与colorWithRed相同的结果:格林:蓝色:α。

But the most interesting part is, if you set translucent property to NO, then it will take the color you specified BUT with alpha 1. So if I specify colorWithRed:1 green:0 blue:0 alpha:0 it will be set to pure red with alpha as 1.

但是最有趣的部分是,如果你将半透明属性设置为NO,那么它将采用你指定的颜色,但是是1。如果我指定colorWithRed:1绿色:0蓝色:0阿尔法:0它将被设置为纯红色,阿尔法为1。

How can I achieve it? Is there any possible solution to make it a completely invisible barTintColor, although it requires hacky methods?

我怎样才能做到呢?是否有可能的解决方案使它成为完全不可见的barTintColor,尽管它需要一些陈腐的方法?

3 个解决方案

#1


4  

If you want a clear navigation controller try setting the background image of the navigation controller to use a clear PNG file (1x1 transparent, no color).

如果您想要一个清晰的导航控制器,请尝试设置导航控制器的背景图像来使用一个清晰的PNG文件(1x1透明,无颜色)。

#2


1  

This works for me

这适合我

navigationBar.translucent = true
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

#3


0  

My answer is very similar to Brian's with a suggestion made by David although it's not the same. I'm posting it so it saves time for anyone trying to do this.

我的回答和布莱恩的答案很相似,大卫的建议虽然不一样。我把它贴出来,这样可以节省时间。

// Forcing transparent background.
navigationBar.translucent = YES;
navigationBar.backgroundImage = [[UIImage alloc] init];

It is important to set translucent to YES. Otherwise the bar will be black. I tested it on iOS 7.1.1.

将半透明设置为YES是很重要的。否则,酒吧将是黑色的。我在ios7.1.1上测试过。

#1


4  

If you want a clear navigation controller try setting the background image of the navigation controller to use a clear PNG file (1x1 transparent, no color).

如果您想要一个清晰的导航控制器,请尝试设置导航控制器的背景图像来使用一个清晰的PNG文件(1x1透明,无颜色)。

#2


1  

This works for me

这适合我

navigationBar.translucent = true
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

#3


0  

My answer is very similar to Brian's with a suggestion made by David although it's not the same. I'm posting it so it saves time for anyone trying to do this.

我的回答和布莱恩的答案很相似,大卫的建议虽然不一样。我把它贴出来,这样可以节省时间。

// Forcing transparent background.
navigationBar.translucent = YES;
navigationBar.backgroundImage = [[UIImage alloc] init];

It is important to set translucent to YES. Otherwise the bar will be black. I tested it on iOS 7.1.1.

将半透明设置为YES是很重要的。否则,酒吧将是黑色的。我在ios7.1.1上测试过。