ios – UINavigationController透明度?

我试图让我的导航栏变为100%透明,这样UINavigationButtonItems只能看到,背景(通常是白色)应该显示背景图像.

我试过了

HomeNavigationController *navBar = [[HomeNavigationController alloc] initWithRootViewController:self.myViewController];
[navBar.navigationBar setBarTintColor:[UIColor clearColor]];
    [navBar.navigationBar setTranslucent:YES];

虽然似乎都不起作用.

编辑:

运用

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                     forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

我可以看到它按预期工作,但现在这些项目也是看不见的.

FINALEDIT:啊,上面的代码可以工作,只是确保你没有先测试就不对外观进行任何其他改动!

实际执行预期的代码:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];

或者,您可以设置透明图像,但这更有意义.如果您不想要代表边框的线,则需要原始编辑中显示的其余代码.

解决方法

有一个技巧.
只需将透明图像设置为导航栏背景即可.
UIImage *fakeImage = [UIImage imageNamed:@"transparentImage"];
[navigationBar setBackgroundImage:fakeImage forBarMetrics:UIBarMetricsDefault];

要么

[navigationBar setBackgroundImage:[UIImage new]
                    forBarMetrics:UIBarMetricsDefault];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...