参考简书
代码.h
#import <UIKit/UIKit.h>
@interface UINavigationBar (Gradient)
- (void)setBGColor:(UIColor *)color;
- (void)setClearColor;
@end
代码.m
#import "UINavigationBar+Gradient.h"
@implementation UINavigationBar (Gradient)
- (void)setBGColor:(UIColor *)color {
//去掉细线
[self setShadowImage:[UIImage new]];
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// self.maskView addSubview:<#(nonnull UIView *)#>
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 20)];
bgView.backgroundColor = color;
[self.subviews.firstObject insertSubview:bgView atIndex:0];
// [self.subviews ];
}
- (void)setClearColor {
[self setBGColor:[UIColor clearColor]];
}
@end