带透明导航栏的可见按钮 [英] Visible buttons with transparent navigation bar
问题描述
我见过几个具有完全透明导航栏但带有可见按钮的应用程序,我似乎无法找到任何不会使按钮不可见的东西。我确定他们正在使用UINavigationController作为导航栏,因为它有与淡入淡出相同的动画,但没有。
I've seen several apps that have completely transparent navigation bars but with visible buttons, I cant seem to find anything that wont make the button invisible as well. I'm sure they were using UINavigationController for the navbar because it had the same animations with fade and what not.
我目前在ViewDidLoad和ViewDidAppear中使用此代码隐藏或显示导航栏,因为它不应该在第一页上 -
Im currently using this code in ViewDidLoad and ViewDidAppear to either hide or show the nav bar because it's not supposed to be on the first page-
[self.navigationController setNavigationBarHidden:NO animated:YES];
此代码的透明度:
[self.navigationController.navigationBar setAlpha:0.0];
推荐答案
创建 UINavigationBar的子类
除 drawRect:
外不包含任何方法。如果需要,可以在那里放置自定义绘图代码,否则将其留空(但实现它)。
Create a subclass of UINavigationBar
containing no methods except drawRect:
. Put custom drawing code there if you need to, otherwise leave it empty (but implement it).
接下来,将 UINavigationController
的导航栏设置为此子类。在代码中使用 initWithNavigationBarClass:toolBarClass:
,或者只是在使用storyboards / nibs时在Interface Builder中更改它(它是侧面层次结构中UINavigationController的子类) )。
Next, set the UINavigationController
's navigation bar to this subclass. Use initWithNavigationBarClass:toolBarClass:
in code, or just change it in the Interface Builder if you're using storyboards/nibs (it's a subclass of your UINavigationController in the hierarchy at the side).
最后,获取对导航栏的引用,以便我们可以使用 self.navigationController.navigationBar
进行配置。在包含的视图控制器的 loadView
中。将导航栏的半透明
设置为 YES
和 backgroundColor
到 [UIColor clearColor]
。示例如下。
Finally, get a reference to your navigation bar so we can configure it using self.navigationController.navigationBar
in the loadView
of the contained view controller. Set the navigation bar's translucent
to YES
and backgroundColor
to [UIColor clearColor]
. Example below.
//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];
}
这是一个屏幕截图结果,模仿瘟疫。
Here's a screen shot of the result, mimicking Plague.
蓝色边框是在 drawRect:
中绘制的,以显示UINavigationBar在那里,而不仅仅是一个按钮和一个标签。我在子类中实现了 sizeThatFits:
以使条形更高。按钮和标签都是UIView,其中包含作为UIBarButtonItems放置在栏中的正确UI元素。我先将它们嵌入到视图中,以便我可以更改它们的垂直对齐方式(否则当我实现 sizeThatFits:
时它们会卡在底部。)
The blue border was drawn in drawRect:
to show you that a UINavigationBar is there and not just a button and a label. I implemented sizeThatFits:
in the subclass to make the bar taller. Both the button and label are UIView's containing the correct UI element that were placed in the bar as UIBarButtonItems. I embedded them in views first so that I could change their vertical alignment (otherwise they "stuck" to the bottom when I implemented sizeThatFits:
).
这篇关于带透明导航栏的可见按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!