2012-11-17 138 views
1

我见过几个应用程序,它们具有完全透明的导航栏,但带有可见按钮,我似乎无法找到任何不会使该按钮不可见的内容。我确定他们使用UINavigationController作为导航栏,因为它具有与淡入淡出相同的动画。透明导航栏的可见按钮

即时通讯目前使用viewDidLoad中和ViewDidAppear此代码隐藏或显示导航栏,因为它不应该是第一个页面级

[self.navigationController setNavigationBarHidden:NO animated:YES]; 

这个代码透明度:

[self.navigationController.navigationBar setAlpha:0.0]; 
+0

我很难想象一个带有可见按钮的不可见导航栏如何看起来不太奇怪。你能给出一个具有你想要的行为的应用程序的例子吗? – Darren

+0

游戏“瘟疫”是我唯一能记住的头脑。怪异全部取决于你如何设计你的界面。 – PappaSmalls

回答

7

创建UINavigationBar的子类,其中不包含除drawRect:之外的其他方法。如果需要,请将自定义绘图代码放在那里,否则将它留空(但实现它)。

接下来,将UINavigationController的导航栏设置为此子类。在代码中使用initWithNavigationBarClass:toolBarClass:,或者如果使用storyboard/nibs(它是侧面层次结构中UINavigationController的子类),则只需在Interface Builder中更改它即可。

最后,获取您的导航栏的引用,以便我们可以在包含的视图控制器的loadView中使用self.navigationController.navigationBar对其进行配置。将导航栏的translucent设置为YESbackgroundColor[UIColor clearColor]。下面的例子。

//CustomNavigationBar.h 
#import <UIKit/UIKit.h> 

@interface CustomNavigationBar : UINavigationBar 
@end 

//CustomNavigationBar.m 
#import "CustomNavigationBar.h" 

@implementation CustomNavigationBar 

- (void)drawRect:(CGRect)rect {} 

@end 

//Put this in the implementation of the view controller displayed by the navigation controller 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.navigationController.navigationBar.translucent = YES; 
    [self navigationController].navigationBar.backgroundColor = [UIColor clearColor]; 
} 

这里的结果的屏幕截图,模仿瘟疫。

enter image description here

蓝色边框吸进drawRect:向您展示一个UINavigationBar的是存在的,不只是一个按钮和一个标签。我在子类中实现了sizeThatFits:以使高度更高。按钮和标签都是UIView的,它包含正确的UI元素,这些UI元素作为UIBarButtonItems放在条中。我首先将它们嵌入到视图中,以便我可以改变它们的垂直对齐(否则当我实施sizeThatFits:时,它们会“卡”到底部)。

+0

如果你的应用运行5.0并且以后使用'UIAppearance'。'drawRect'不适用于iOS 5或更高版本。 – nanjunda

+0

你确定吗? 'drawRect:'在iOS 5中被调用,只要你在*子类*中实现它(如果你调用它,它不会),所以这应该是自定义绘图的罚款,不应该吗?我现在在iOS 5.1模拟器中使用它,它工作正常。调用'drawRect:'并且该条是完全透明的。 – Metabble

+0

@nanjunda我刚刚在导航栏中实现了'drawRect:'自定义绘图,并且它在iOS 5中工作得很好。除非文档说不要这么做,否则没关系。 Apple甚至告诉我们将'UINavigationBar'子类化为使用'drawRect:',并且它在iOS 5中不再起作用。为什么不告诉我们它不起作用?无论您是否绘制,您必须实现'drawRect:'才能使其工作,否则将UINavigationBar的背景色设置为清除会产生黑色或半透明灰色条,具体取决于“半透明”是否具有已设置。 – Metabble

1

为了使导航栏透明,使用以下代码:

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

在此之后设置的导航栏的背景图像中的与使用以下属性它后面的视图:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"SAMPLE.jpg"] forBarMetrics:UIBarMetricsDefault]; 
1
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black) 
const float colorMask[6] = {222, 255, 222, 255, 222, 255}; 
UIImage *img = [[UIImage alloc] init]; 
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)]; 

[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault]; 
//remove shadow 
    [[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];