如何通过调用方法设置导航栏颜色?
how to set navigationBar color by calling a method?
我创建了 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;
你的代码有小错误
+(void)setNavBackground:(id)viewController Color:(UIColor *)color
{
UIViewController *controller = (id)viewController;
controller.navigationController.navigationBar.barTintColor = color; // Change NavigationBar color to Red
}
像这样调用方法:[CommonMethod setNavBackground:self颜色:[UIColor redColor]];
我创建了 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;
你的代码有小错误
+(void)setNavBackground:(id)viewController Color:(UIColor *)color
{
UIViewController *controller = (id)viewController;
controller.navigationController.navigationBar.barTintColor = color; // Change NavigationBar color to Red
}
像这样调用方法:[CommonMethod setNavBackground:self颜色:[UIColor redColor]];