【发布时间】:2011-01-28 22:57:26
【问题描述】:
我尝试了以下代码,但它不起作用。
[helloToolbar setBackgroundColor:[UIColor clearColor]];
【问题讨论】:
标签: cocoa-touch
我尝试了以下代码,但它不起作用。
[helloToolbar setBackgroundColor:[UIColor clearColor]];
【问题讨论】:
标签: cocoa-touch
你能做的最好的就是使用
[helloToolbar setBarStyle:UIBarStyleBlack];
[helloToolbar setTranslucent:YES];
这将为您提供一个黑色但半透明的工具栏。
【讨论】:
要制作一个完全透明的工具栏,请使用方法described here。简而言之,创建一个继承自 UIToolbar 的新 TransparentToolbar 类,并使用它来代替 UIToolbar。
TransarentToolbar.h
@interface TransparentToolbar : UIToolbar
@end
TransarentToolbar.m
@implementation TransparentToolbar
// Override draw rect to avoid
// background coloring
- (void)drawRect:(CGRect)rect {
// do nothing in here
}
// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
{
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.translucent = YES;
}
// Override init.
- (id) init
{
self = [super init];
[self applyTranslucentBackground];
return self;
}
// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
[self applyTranslucentBackground];
return self;
}
@end
(上面链接的博客文章中的代码)
【讨论】:
另一种解决方案是为 UIToolbar 定义一个类别:
@implementation UIToolbar(Transparent)
-(void)drawRect:(CGRect)rect {
// do nothing in here
}
@end
在 IB 中将工具栏设置为黑色半透明且不透明。
【讨论】:
我们刚刚注意到覆盖 drawRect 不再适用于 iOS 4.3。它不再被调用(编辑:似乎只在模拟器中)。而是调用 drawLayer:inContext:。
here 发布了一个很好的解决方案
现在您可以将每个 UIToolbar 对象设置为透明,方法是将其 tintColor 设置为 [UIColor clearColor] :)
【讨论】:
我刚刚在模拟器和手机上使用 iOS 4.3 测试了以下内容,似乎工作正常。子类 UIToolbar,提供一种方法:
- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
【讨论】:
toolbar.barStyle = UIBarStyleBlackTranslucent;
【讨论】:
感谢@morais 的解决方案 - 这是转换为 MonoTouch 的代码:
public class TransparentToolbar : UIToolbar
{
public TransparentToolbar()
{
init();
}
public TransparentToolbar(RectangleF frame) : base(frame)
{
init();
}
void init()
{
BackgroundColor=UIColor.Clear;
Opaque=false;
Translucent=true;
}
public override void Draw(RectangleF rect)
{
}
}
【讨论】:
在 iOS 5 中,以下工作:
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectZero];
if (bar.subviews.count > 0)
[[[bar subviews] objectAtIndex:0] removeFromSuperview];
这是因为背景现在是子视图。即使在 iOS 的新迭代中,此代码也是安全的,但它可能会停止工作。这不是私有 API 使用,您的应用可以安全地提交到商店。
确保在将任何UIBarButtonItems 添加到栏之前删除背景视图。否则我的代码将无法正常工作。
【讨论】:
在 iOS 5 中,只需调用 setBackgroundImage 并传递一个透明图像。
我是这样做的(我动态生成透明图像):
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[toolbar setBackgroundImage:transparentImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
【讨论】:
[toolbar setBackgroundImage:[UIImage alloc] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
这可以在 iOS5.1 中以极少的努力工作。我正在匹配大小,因为只有背景与工具栏本身具有相同的框架大小。当然,您可以使用其他标准。
享受吧。
如下创建一个UIToolbar的子类:
.h:
#import <UIKit/UIKit.h>
@interface UIClearToolbar : UIToolbar
@end
.m:
#import "UIClearToolbar.h"
@implementation UIClearToolbar
- (void)layoutSubviews {
// super has already laid out the subviews before this call is made.
[self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
if (CGSizeEqualToSize(self.frame.size, obj.frame.size) ||
self.frame.size.width <= obj.frame.size.width) { // on device, the background is BIGGER than the toolbar.) {
[obj removeFromSuperview];
*stop = YES;
}
}];
}
@end
【讨论】:
透明(iOS 5.0):
[toolbar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
半透明:
[toolbar setBarStyle:UIBarStyleBlack];
[toolbar setTranslucent:YES];
【讨论】:
适用于所有设备的累积解决方案,从最旧的 iOS 3.0 (iPhone 1) 到最新的 iOS 6.1 (iPad mini)。
@implementation UIToolbar (Extension)
- (void)drawRect:(CGRect)rect
{
if (CGColorGetAlpha(self.backgroundColor.CGColor) > 0.f)
{
[super drawRect:rect];
}
}
- (void)setTransparent
{
//iOS3+
self.backgroundColor = [UIColor clearColor];
//iOS5+
if ([self respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
{
[self setBackgroundImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
}
//iOS6+
if ([self respondsToSelector:@selector(setShadowImage:forToolbarPosition:)])
{
[self setShadowImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny];
}
}
@end
如果您想要一个透明的工具栏,请在上面调用setTransparent。
当您想要一个不透明的工具栏时,设置您选择的背景颜色或自己添加一个 imageView。
【讨论】: