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;
}