#import "MyNavigationController.h"
@interface MyNavigationController ()
@end
@implementation MyNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
+(void)initialize
{
// 1.获得bar对象
UINavigationBar *navBar = [UINavigationBar appearance];
// 2.1.设置bar背景
// 设置导航栏背景图片
// [navBar setBackgroundImage:[UIImage imageNamed:@"viewBg"] forBarMetrics:UIBarMetricsDefault];
// 设置导航栏背景色
[navBar setBarTintColor:[UIColor orangeColor]];
// 要保持默认的透明传统效果,需要设置下面2个方法
[navBar setTranslucent:YES];
[navBar setShadowImage:[[UIImage alloc] init]];
// 这个方法可以不写
[navBar setBackgroundColor:[UIColor clearColor]];
}
@end
延伸阅读:
以前一直没用过带透明的导航栏图片,现在项目要用到这样的。以为放上图片就是自动透明了
可是,发现透明那部竟然是黑的。无论怎么clearcolor 都不行。百度了很多页也没有查到可以用的方法。
最后在http://stackoverflow.com 找到一个关键点 :
viewDidLoad
。导航自定义图片设置这里也顺便贴出来吧
- (void) setNavBarImg:(UINavigationBar *)navBar
{
#define kSCNavBarImageTag 10
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
//if iOS 5.0 and later
[navBar setBackgroundImage:ThemeImageName(@"bg_nav@2x") forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];
[imageView setBackgroundColor:ClearColor];
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
ThemeImageName(@"bg_nav@2x")];
[imageView setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];
}
}
}如果你的图片是有透明的。如果你在百度里搜。那你真的要搜死了。。
方法:
[self.navigationController.navigationBar setTranslucent:YES];
// 为什么要加这个呢,shadowImage 是在ios6.0以后才可用的。但是发现5.0也可以用。不过如果你不判断有没有这个方法,
// 而直接去调用可能会crash,所以判断下。作用:如果你设置了上面那句话,你会发现是透明了。但是会有一个阴影在,下面的方法就是去阴影
if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])
{
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
// 以上面4句是必须的,但是习惯还是加了下面这句话
[self.navigationController.navigationBar setBackgroundColor:ClearColor];
本文详细介绍了如何在iOS应用中实现带有透明效果的导航栏。通过设置UINavigationBar的属性,包括背景颜色、透明度及阴影图片等,解决了图片透明部分显示为黑色的问题,并提供了兼容不同iOS版本的解决方案。
3万+

被折叠的 条评论
为什么被折叠?



