如何通过调用方法设置navigationBar颜色?

时间:2015-09-09 06:10:51

标签: ios objective-c uinavigationbar subclassing

我创建了一个NSObject的CommonMethod子类来自定义UINavigationBar,这里是CommonMethod.h和CommonMethod.m文件

CommonMethod.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface CommonMethods : NSObject
+(void)setNavBackButton:(id)viewController;
+(void)setNavBackground:(id)viewController Color:(UIColor *)color;
@end

CommonMethod.h

#import <UIKit/UIKit.h>
#import "CommonMethods.h"
@implementation CommonMethods
+(void)setNavBackground:(id)viewController Color:(UIColor *)color
{
    UIViewController *controller = (id)viewController;
    controller.navigationController.navigationBar.backgroundColor=[UIColor redColor];
}
@end

现在,当我调用[CommonMethods setNavBackground:self Color:[UIColor redColor];时,它正在执行该方法,但没有在UI上进行任何更改。任何人都可以赦免我所缺少的东西吗?

编辑:

我也试过controller.navigationController.navigationBar.barTintColor = color;

1 个答案:

答案 0 :(得分:1)

您在代码上有轻微的错误

+(void)setNavBackground:(id)viewController Color:(UIColor *)color
{
    UIViewController *controller = (id)viewController;
    controller.navigationController.navigationBar.barTintColor = color; // Change NavigationBar color to Red
}

这样的调用方法:[CommonMethod setNavBackground:self Color:[UIColor redColor]];

enter image description here