自定义的导航控制中设置保留系统默认的透明传统效果

来源:互联网 发布:mysql 渗透 编辑:程序博客网 时间:2024/05/18 04:57
#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];


0 0