关于修改导航栏背景方法

现在的应用基本上已经告别了原生的导航栏,都是更倾向根据自己应用的风格来定义自己的导航栏,或者说去掉了导航栏,今天我们就来聊一聊如何去修改导航的问题。

第一种方法,隐藏导航栏,navigationBarHidden = YES;然后通过设置图片来替代导航栏。(这个就不细说了)

第二种方法,设置导航栏的背景,在5.0版本之前一般我都需要通过drawRect方法来绘制背景,在ios5.0之后,苹果提供 [self setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]这个方法,

可以直接来设置背景,下面就让我们来看下。

#import "UINavigationBar+BgImage.h"

@implementation UINavigationBar(BgImage)

- (void)drawRect:(CGRect)rect
{
    UIImage *image = [UIImage imageNamed: @"bgImage.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    if (self.topItem.title.length>0) {
        UILabel *alabel=[[UILabel alloc] initWithFrame:CGRectMake(85, 0, 160, 44)];
        alabel.textColor=[UIColor whiteColor];
        alabel.shadowColor=[UIColor redColor];
        alabel.shadowOffset=CGSizeMake(0, 1);
        alabel.font=[UIFont boldSystemFontOfSize:16];
        alabel.backgroundColor=[UIColor clearColor];
        alabel.textAlignment=UITextAlignmentCenter;
        alabel.text=self.topItem.title;
        self.topItem.titleView=alabel;
        [alabel release];
    }
}

- (void)setNavigationBar:(UIImage *)backgroundImage{
    
    if([[[UIDevice currentDevice] systemVersion] floatValue]<5.0){
        [self setNeedsDisplay];
    }else{
        
        [self setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
        
        CGSize  offset=CGSizeMake(0, 1);
        NSValue * avalue=[NSValue valueWithCGSize:offset];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
                           [UIFont boldSystemFontOfSize:19], UITextAttributeFont,
                           avalue, UITextAttributeTextShadowOffset,
                           [UIColor colorWithRed:201.0f/255.0f green:74.0f/255.0f blue:5.0f/255.0f alpha:1.0f], UITextAttributeTextShadowColor,
                           [UIColor whiteColor], UITextAttributeTextColor,
                           nil];
        self.titleTextAttributes=dic;
        
    }
}

我们在调用的时候只需直接调用setNavigationBar即可;

话说到这里还没完,之后又发现一种很类似的方法,貌似又简单了不少

#import <UIKit/UIKit.h>

@interface UINavigationBar (CustomBackgroundImage)

@property (nonatomic, retain) UIImage *backgroundImage;

@end

#import "UINavigationBar+CustomBackgroundImage.h"
#import <objc/runtime.h>

static char EKNavigationBarKey;

@implementation UINavigationBar (CustomBackgroundImage)

- (void)drawRect:(CGRect)rect
{
    if (self.backgroundImage)
        [self.backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    else
        [super drawRect:rect];
}

- (UIImage *)backgroundImage
{
    if ([self respondsToSelector:@selector(backgroundImageForBarMetrics:)])
        return [self backgroundImageForBarMetrics:UIBarMetricsDefault];
    else
        return objc_getAssociatedObject(self, &EKNavigationBarKey);
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
        [self setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
    else
        objc_setAssociatedObject(self, &EKNavigationBarKey, backgroundImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

原文地址:https://www.cnblogs.com/CafeWing/p/3092501.html