带有透明导航栏的可见按钮

Posted

技术标签:

【中文标题】带有透明导航栏的可见按钮【英文标题】:Visible buttons with transparent navigation bar 【发布时间】:2012-11-17 15:28:12 【问题描述】:

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

我目前在 ViewDidLoad 和 ViewDidAppear 中使用此代码来隐藏或显示导航栏,因为它不应该在第一页上-

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

这个代码的透明度:

[self.navigationController.navigationBar setAlpha:0.0];

【问题讨论】:

我很难想象一个带有可见按钮的不可见导航栏看起来并不奇怪。您能否举一个具有您想要的行为的应用示例? 游戏“瘟疫”是我脑海中唯一记得的游戏。奇怪之处完全取决于您如何设计界面。 【参考方案1】:

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

接下来,将UINavigationController 的导航栏设置为此子类。在代码中使用initWithNavigationBarClass:toolBarClass:,或者如果您使用故事板/笔尖,则只需在界面生成器中更改它(它是侧面层次结构中 UINavigationController 的子类)。

最后,获取导航栏的引用,以便我们可以在包含的视图控制器的loadView 中使用self.navigationController.navigationBar 对其进行配置。将导航栏的translucent 设置为YES,将backgroundColor 设置为[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];


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

drawRect: 中绘制了蓝色边框,以向您显示 UINavigationBar 存在,而不仅仅是按钮和标签。我在子类中实现了sizeThatFits: 以使条更高。按钮和标签都是 UIView,包含作为 UIBarButtonItems 放置在栏中的正确 UI 元素。我首先将它们嵌入到视图中,以便我可以更改它们的垂直对齐方式(否则当我实现 sizeThatFits: 时它们“卡”在底部)。

【讨论】:

如果您的应用运行 5.0 及更高版本,请使用 UIAppearancedrawRect 不适用于 ios 5 或更高版本。 你确定吗? drawRect: 在 iOS 5 中被调用,只要你在 subclass 中实现它(如果你调整它,它不会)所以这对于自定义绘图应该没问题,不是吗?我现在在 iOS 5.1 模拟器中使用一个,它工作正常。 drawRect: 被调用并且栏是完全透明的。 @nanjunda 我刚刚在导航栏中实现了drawRect:for 自定义绘图,它在 iOS 5 中运行良好。除非文档说不这样做,否则没关系。 Apple 甚至告诉我们将 UINavigationBar 子类化以使用 drawRect: 并且在 iOS 5 中对其进行调配不再适用。为什么不告诉我们它不起作用呢?无论您是否绘制,都必须实现 drawRect: 才能使其正常工作,否则将 UINavigationBar 的背景颜色设置为清除会产生黑色或半透明的灰色条,具体取决于 translucent 是否已经设置。 这样的事情会不会导致应用被拒绝,或者它是否足够接近苹果指南?【参考方案2】:
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]];

【讨论】:

【参考方案3】:

要使导航栏透明,请使用以下代码:

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];

【讨论】:

以上是关于带有透明导航栏的可见按钮的主要内容,如果未能解决你的问题,请参考以下文章

Matplotlib 中的透明导航栏(或者,可以在没有栏的情况下添加导航按钮吗?)

带有透明/模糊导航栏的 iOS 7 视图控制器布局问题

带有半透明导航栏的滚动条

iOS 11:带有不透明导航栏的导航控制器内带有滚动视图的弹出视图控制器在转换期间导致奇怪的内容动画

只有返回按钮和透明背景的导航

仅使用后退按钮和透明背景导航