#import <UIKit/UIKit.h>
@interface UINavigationBar (Alpha)
/**
* 重新设置navigtionbar的透明度
*/
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor;
- (void)lt_setContentAlpha:(CGFloat)alpha;
- (UIStatusBarStyle)preferredStatusBarStyle;
@end
#import "UINavigationBar+Alpha.h"
#import <objc/runtime.h>
static char overlayKey;
static char emptyImageKey;
@implementation UINavigationBar (Alpha)
-(UIView *)overlay;
{
return objc_getAssociatedObject(self,&overlayKey);
}
- (void)setOverlay:(UIView *)overlay
{
objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)emptyImage
{
return objc_getAssociatedObject(self, &emptyImageKey);
}
- (void)setEmptyImage:(UIImage *)image
{
objc_setAssociatedObject(self, &emptyImageKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
if (!self.overlay) {
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self setShadowImage:[UIImage new]];
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 64)];
self.overlay.userInteractionEnabled = NO;
self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:self.overlay atIndex:0];
}
self.overlay.backgroundColor = backgroundColor;
}
- (void)setAlpha:(CGFloat)alpha forSubviewsOfView:(UIView *)view
{
for (UIView *subview in view.subviews) {
if (subview == self.overlay) {
continue;
}
subview.alpha = alpha;
[self setAlpha:alpha forSubviewsOfView:subview];
}
}
- (void)lt_setContentAlpha:(CGFloat)alpha
{
if (!self.overlay) {
[self lt_setBackgroundColor:self.barTintColor];
}
[self setAlpha:alpha forSubviewsOfView:self];
if (alpha == 1) {
if (!self.emptyImage) {
self.emptyImage = [UIImage new];
}
self.backIndicatorImage = self.emptyImage;
}
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}