如何使UIToolbar具有透明背景?

16

我有一个UIToolbar,使用白色色调,里面有一个bar button item,接着是一些可伸缩的空间,然后是另一个bar button item。我想把这个工具栏完全变为透明,这样我就可以看到可伸缩空间下面的东西(我不关心按钮后面的东西)。 有什么方法可以实现吗? 我已经尝试将工具栏设置为半透明,但这并不能使其完全透明。


1
可能是透明UIToolbar的重复问题。 - trojanfoe
http://stackoverflow.com/a/7027807/653513 - Rok Jarc
大家好,对于重复的问题我感到抱歉。我没有注意到已经有了另一个相同的问题。请关闭这个问题并标记为重复。 - Kyle Rosenbluth
8个回答

65
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

 [self.toolbar setBackgroundColor:[UIColor clearColor]];

当将透明的UIToolbar插入到NavigationItem.TitleView中时,这种方法是唯一有效的。 - Miros
3
在iOS 7.1上,我看到了一个在工具栏顶部的1像素边框(阴影?)。 - Chris
@Chris,我注意到了相同的问题。不确定如何修复。幸运的是,我需要在弹出窗口中执行此操作,其中视图的边框颜色为黑色。因此,我只是将工具栏向上移动,以便阴影(或其他)丢失在边框中。 - Andrew
10
我使用 setShadowImage:forToolbarPosition: 来设置阴影图片为 [[UIImage alloc] init]。 - Chris
注意:如果将工具栏添加到场景中,则不会通过self.navigationController.toolbar进行引用 - 这是由navigationController拥有的默认工具栏(请参见https://dev59.com/sV7Va4cB1Zd3GeqPNtkn#8753847)。 - Matt
Swift 版本:self.toolbar.isTranslucent = true self.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: UIBarPosition.any, barMetrics: UIBarMetrics.default) self.toolbar.setShadowImage(UIImage(), forToolbarPosition: UIBarPosition.any) - Vivek

8

继承UIToolbar类,并实现以下方法:

- (void)drawRect:(CGRect)rect 
{
  [[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
  CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}

see more details here


那么这是一个重复的问题吗? - trojanfoe
关闭该问题并将其标记为您链接的重复问题。 - trojanfoe
这确实是正确的方法。但很遗憾,如果不进行子类化,它就无法定制... - MikeS
您可以在不使用子类化的情况下进行自定义 - 请参见@NANNAV下面的观点。在我看来,更加优雅,但请注意-1不适合于UIInterface.h中的UIBarStyle枚举。但是可能会出现什么问题呢?:S - Scott Corscadden
非常适合我的情况,但并不适用于每个人。 - Vladimir Stazhilov

5

将工具栏样式设置为-1,就像这样:

 tools.barStyle = -1; // clear background

在我的iOS 11上可以运行! - Alex Black
这是我在 Xcode 13 和 iOS 15 中唯一有效的方法。但正如其他帖子所提到的,它会在工具栏顶部留下一个 1 像素的黑色条。 - LancDec
这在浅色模式下运行良好,但在深色模式下不起作用,必须使用推荐的方法(同时使用setShadow)来完全修复此问题。 - Tj3n
Xcode 15 beta 5 -> 无法将类型为 'Int' 的值分配给类型 'UIBarStyle',似乎无法绕过这个问题... - benc

2

很抱歉,我找到的目前在iOS 7和iOS 6中可靠运行的唯一方法是这样的:

[[toolbar.subviews objectAtIndex:0] removeFromSuperview];

1
始终优先使用 firstObject 而不是 objectAtIndex:0 - Laszlo
谢谢@Tuss。为自己辩护,在iOS 6中,firstObject是一个私有API。;) - Danyal Aytekin

1
如果您想要一个全局的解决方案,请利用 UIAppearance 代理: UIToolbar *toolbarAppearance = [UIToolbar appearance]; [toolbarAppearance setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

1

在Swift 5.1中,@ievgen的回答提供了一种解决方案。

toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
toolbar.backgroundColor = .clear

如果您想清除分隔线灰线,请使用以下代码:

<如果您想清除分隔线灰线,请使用以下代码:/ p></ p>

toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)

0

在iOS 6+中,可以通过将属性translucent设置为“YES”来完成,而无需进行子类化。

这在iOS 5及以下版本中不起作用。以下是在不进行子类化工具栏的情况下完成此操作的方法:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

-1

被接受的答案的Swift 3版本:

   self.toolbar.isTranslucent = true
    self.toolbar.setBackgroundImage(UIImage(),
                               forToolbarPosition: UIBarPosition.any,
                               barMetrics: UIBarMetrics.default)

    self.toolbar.setShadowImage(UIImage(), forToolbarPosition: UIBarPosition.any)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接