我有一个UIToolbar,使用白色色调,里面有一个bar button item,接着是一些可伸缩的空间,然后是另一个bar button item。我想把这个工具栏完全变为透明,这样我就可以看到可伸缩空间下面的东西(我不关心按钮后面的东西)。 有什么方法可以实现吗? 我已经尝试将工具栏设置为半透明,但这并不能使其完全透明。
我有一个UIToolbar,使用白色色调,里面有一个bar button item,接着是一些可伸缩的空间,然后是另一个bar button item。我想把这个工具栏完全变为透明,这样我就可以看到可伸缩空间下面的东西(我不关心按钮后面的东西)。 有什么方法可以实现吗? 我已经尝试将工具栏设置为半透明,但这并不能使其完全透明。
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: UIBarPosition.any)
- Vivek继承UIToolbar类,并实现以下方法:
- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
see more details here
UIInterface.h
中的UIBarStyle枚举。但是可能会出现什么问题呢?:S - Scott Corscadden将工具栏样式设置为-1,就像这样:
tools.barStyle = -1; // clear background
很抱歉,我找到的目前在iOS 7和iOS 6中可靠运行的唯一方法是这样的:
[[toolbar.subviews objectAtIndex:0] removeFromSuperview];
firstObject
而不是 objectAtIndex:0
。 - LaszlofirstObject
是一个私有API。;) - Danyal AytekinUIAppearance
代理:
UIToolbar *toolbarAppearance = [UIToolbar appearance];
[toolbarAppearance setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
在Swift 5.1中,@ievgen的回答提供了一种解决方案。
toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
toolbar.backgroundColor = .clear
<如果您想清除分隔线灰线,请使用以下代码:/ p></ p>
toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
在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];
被接受的答案的Swift 3版本:
self.toolbar.isTranslucent = true
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.any,
barMetrics: UIBarMetrics.default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: UIBarPosition.any)