iOS - [UIColor clearColor] 和 UIToolbars [英] iOS - [UIColor clearColor] and UIToolbars

查看:31
本文介绍了iOS - [UIColor clearColor] 和 UIToolbars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将 [UIColor clearColor]UIToolbar 一起使用,试图使自定义控件界面更适合机械"应用程序(想想按钮你会在 70 年代的电影中看到).

I've been attempting to use [UIColor clearColor] with UIToolbar in an attempt to make a custom control interface more fitting of a "Mechanical" application (Think buttons you would see in a Movie from the 70s).

发生的事情是,当我将工具栏设置为 clearColor 时,它会将其变成哑光黑色.它背后的图像是红色、棕褐色和黑色,所以我确定它没有按预期工作.

What is happening is that when I set the toolbar to clearColor it is turning it matte black. The image behind it is red, tan and black so I'm sure it's not working as intended.

我看到的一个区别是我使用的是导航控制器上的工具栏,而不是独立的 UIToolbar.

One difference I see is that I'm using the toolbar on a nav controller and not a stand alone UIToolbar.

代码行

self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];

而我的上部导航栏(在另一个视图中设置)是 UIBarStyleBlackTranslucent,这会被抛弃吗?

and my upper navigation bar (that is setup in another view) is UIBarStyleBlackTranslucent, could this be throwing it off?

任何帮助追踪此事都会很棒.

any help tracking this down would be great.

推荐答案

您可以使用以下代码为导航控制器的工具栏设置透明背景:

You can set a transparent background for the toolbar of your navigation controller with the following code:

// UIToolbar.h
@interface UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect;
@end
// UIToolbar.m
#import "TransparentToolbar.h"
@implementation UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect {
    [[UIColor clearColor] set];
    CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
@end

用法:

// bar_bottom_bumped.png is a toolbar image with transparency
UIImage *bg = [UIImage imageNamed:@"bar_bottom_bumped.png"];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = self.navigationController.toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];
[self.navigationController.toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];

这篇关于iOS - [UIColor clearColor] 和 UIToolbars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆