【发布时间】:2011-09-09 04:14:07
【问题描述】:
我编写了以下代码来使我的工具栏透明。
[mtoolbar setBackgroundColor:[UIColor clearColor]];
如何使UIToolbar 透明?
【问题讨论】:
-
您想要一个真正透明的工具栏,还是只是一个半透明的?
标签: iphone transparent uitoolbar
我编写了以下代码来使我的工具栏透明。
[mtoolbar setBackgroundColor:[UIColor clearColor]];
如何使UIToolbar 透明?
【问题讨论】:
标签: iphone transparent uitoolbar
您可以将属性translucent 设置为YES 看看是否有帮助。
【讨论】:
查看以下代码
[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];
取自
@Brandon Bodnár 已在以下 SO 帖子中回答。
Couldn't UIToolBar be transparent?
你也可以使用不同的方法
【讨论】:
以下适用于 iOS 5(和 iOS 6 beta 4,尽管仍然可以看到轻微的顶部阴影)。
请注意: 使 UIToolbar 或 UINavigationBar 透明并不是一个好主意,以这种方式修改 Apple 的 UIKit 元素迟早会失败。
TransparentToolbar.h
#import <UIKit/UIKit.h>
@interface TransparentToolbar : UIToolbar
@end
TransparentToolbar.m
#import "TransparentToolbar.h"
@implementation TransparentToolbar
-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
// This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
if (index != 0)
{
[super insertSubview:view atIndex:index];
}
else
{
// insert your custom background view, if you want to
}
}
@end
编辑:在 iOS 5+ 中,也可以简单地设置 backgroundImage(可以是透明的)。这当然是“更清洁”的解决方案,但不如自定义 UIView 灵活。
[someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
【讨论】:
for (UIView * sv in [toolBar subviews])
{
[sv removeFromSuperview];
}
;) 任何 iO
【讨论】:
将属性 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];
【讨论】:
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
【讨论】:
这适用于 iOS 6 和 7:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
【讨论】: