您的位置:首页 > 其它

重新设置navigtionbar的透明度和颜色的方法

2016-01-14 17:42 288 查看
#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
{
//为navigation关联一个view对象
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;
}
}

//statusBar设置成白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
//also you may add any fancy condition-based code here
return UIStatusBarStyleLightContent;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: