1

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

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

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

这个代码的透明度:

[self.navigationController.navigationBar setAlpha:0.0];
4

3 回答 3

7

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

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

最后,获取导航栏的引用,以便我们可以在包含的视图控制器中配置self.navigationController.navigationBarloadView。将导航栏设置translucentYES和。下面的例子。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:)。

于 2012-11-17T17:22:31.947 回答
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];
于 2013-08-27T05:23:17.173 回答
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]];
于 2013-09-12T04:33:18.500 回答