iOS 导航栏透明色

1、首先给UINavigationController添加一个扩展类 添加一个属性

.h文件

@interfaceUINavigationController (navController)

@property(nonatomic,copy)NSString*navColorAlpha;

@end

2、利用runtime机制添加属性

#import  objc/runtime.h

@implementationUINavigationController (navController)

staticcharnarColorAlphakey;

-(NSString*)navColorAlpha

{

returnobjc_getAssociatedObject(self, &narColorAlphakey);

}

-(void)setNavColorAlpha:(NSString*)navColorAlpha

{

objc_setAssociatedObject(self, &narColorAlphakey, navColorAlpha,OBJC_ASSOCIATION_COPY_NONATOMIC);

[selfsetNeedsNavigationBackground:[navColorAlphafloatValue]];

}

3、去分解导航的层数 并设置为alpha= 0

-(void)setNeedsNavigationBackground:(CGFloat)alpha {

// 导航栏背景透明度设置

UIView*barBackgroundView = [[self.navigationBarsubviews]objectAtIndex:0];// _UIBarBackground

UIImageView*backgroundImageView = [[barBackgroundViewsubviews]objectAtIndex:0];// UIImageView

if(self.navigationBar.isTranslucent) {

if(backgroundImageView !=nil&& backgroundImageView.image!=nil) {

barBackgroundView.alpha= alpha;

}else{

UIView*backgroundEffectView = [[barBackgroundViewsubviews]objectAtIndex:1];// UIVisualEffectView

if(backgroundEffectView !=nil) {

backgroundEffectView.alpha= alpha;

}

}

}else{

barBackgroundView.alpha= alpha;

}

self.navigationBar.clipsToBounds= alpha ==0.0;

}


注:导航的层级关系

iOS 导航栏透明色_第1张图片
导航栏的层级 关系

希望能帮到你

你可能感兴趣的:(iOS 导航栏透明色)