gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:23:33 25 4
gpt4 key购买 nike

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

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

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

这个代码的透明度:
[self.navigationController.navigationBar setAlpha:0.0];

最佳答案

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

接下来,设置UINavigationController的导航栏到这个子类。使用initWithNavigationBarClass:toolBarClass:在代码中,或者如果您使用 Storyboard/ Nib ,则只需在界面生成器中更改它(它是侧面层次结构中 UINavigationController 的子类)。

最后,获取导航栏的引用,以便我们可以使用 self.navigationController.navigationBar 对其进行配置在 loadView包含的 View Controller 。设置导航栏的translucentYESbackgroundColor[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,包含作为 UIBarButtonItems 放置在栏中的正确 UI 元素。我首先将它们嵌入到 View 中,以便我可以更改它们的垂直对齐方式(否则当我实现 sizeThatFits: 时它们“卡”在底部)。

关于objective-c - 带有透明导航栏的可见按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431976/

25 4 0