iOS开发中,UINavigationController是一种常用的视图控制器,主要用来控制UI界面流程跳转。
在UINavigationController使用过程中,经常会遇到导航栏(UINavigationBar)显示/隐藏、透明/不透明,以及透明渐变的问题。
本文主要介绍导航栏(UINavigationBar)显示/隐藏、透明/不透明,以及透明渐变的问题。
以下是我整理的与导航栏相关的控件组织图:
通过上图可以看出:
1、UINavigationController继承于UIViewController;
2、UINavigationController包含viewControllers(UIViewController数组)、UINavigationBar、UIToolbar;
3、UINavigationBar管理items(UINavigationItem数组);
4、UIViewController包含UINavigationItem。
下图能更好的理解UINavigationController组织结构
隐藏与显示
方法一:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController
@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
@property(nonatomic,readonly) UINavigationBar *navigationBar;
@end
使用UINavigationController的navigationBarHidden属性获取隐藏导航方法。常用于以下方法中
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
当然还有实现UINavigationControllerDelegate,在代理方法中通过判断showViewController类型,来控制显示。
// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
这种隐藏方式在滑动过程中,有些iOS版本会出现过渡不自然现象。(现不建议使用)
效果如下:
方法二:
使用UINavigationController+FDFullscreenPopGesture
该类重写了UINavigationController的+ (void)load;
方法。
具体可参照以下代码:
+ (void)load {
// Inject "-pushViewController:animated:"
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(pushViewController:animated:);
SEL swizzledSelector = @selector(fd_pushViewController:animated:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
运用了runtime技术,在执行-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
时,执行- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
,以此来解决滑动过程中,过渡不自然问题。
该方法有一个不算缺陷的缺陷,就是一个工程只能只能置换一次。
如果你在一个SDK中用到了该技术(修改类别名),并且另一个工程引入了该SDK,并且也使用了该技术。会造成滑动不自然。当然,你可以把该技术单独提出来,供SDK和工程共同引用,这样就可以解决问题了。
透明与不透明,以及透明渐变
方法一:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationBar : UIView <NSCoding, UIBarPositioning>
/*
New behavior on iOS 7.
Default is YES.
You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
*/
// Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
@end
方法二:
通过文章刚开始介绍的UINavigationController组织图,可发现UINavigationController中只包含一个UINavigationBar。那么我们可以从UINavigationBar直接入手。
self.edgesForExtendedLayout = UIRectEdgeTop;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)]) {
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.alpha = 0.0;
self.navigationController.navigationBar.backItem.hidesBackButton = YES;
self.navigationController.navigationItem.hidesBackButton = YES;
self.navigationItem.hidesBackButton = YES;
该方法是直接控制UINavigationBar的背景图片、阴影图片、背景色、navigationItem等,也能达到类似- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
效果。
常用于以下方法中
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
当然还有实现UINavigationControllerDelegate,在代理方法中通过判断showViewController类型,来控制显示。
// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
这种方法好处是直接控制UINavigationBar,不好的地方是调用的方法过多,还要考虑viewControllers中的navigationItem。
透明渐变
效果图
主要实现思路代码
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.y;
[self setLifeNav:offset];
}
#pragma mark - Nav Bar
- (CGFloat)navBarColorAlpha:(CGFloat)offsetY {
CGFloat alpha;
CGFloat height = 200.0;
if (offsetY <= 0) {
alpha = 0.0;
} else if (offsetY > 0 && offsetY < height) {
alpha = offsetY / height;
} else {
alpha = 1.0;
}
return alpha;
}
/// 使用颜色填充图片
- (UIImage *)imageWithColor:(UIColor *)color
{
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}
- (void)setLifeNav:(CGFloat)offset {
CGFloat maxHeight;
if (@available(iOS 11.0, *)) {
UIEdgeInsets insets = [[UIApplication sharedApplication] keyWindow].safeAreaInsets;
maxHeight = 200.0 - MAX(insets.top, 20.0) - 44.0;
}else {
maxHeight = 200.0 - 64.0;
}
if (offset < maxHeight) {
if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabC = (UITabBarController *)self.window.rootViewController;
for (UINavigationController *navC in tabC.viewControllers) {
if ([navC.topViewController isKindOfClass:[LifeViewController class]]) {
CGFloat alpha = [self navBarColorAlpha:offset];
UIImage *image = [self imageWithColor:[UIColor colorWithRed:60/255.0 green:131/255.0 blue:255/255.0 alpha:alpha]];
[navC.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
break;
}
}
}
}else {
if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabC = (UITabBarController *)self.window.rootViewController;
for (UINavigationController *navC in tabC.viewControllers) {
if ([navC.topViewController isKindOfClass:[LifeViewController class]]) {
UIImage *image = [self imageWithColor:[UIColor colorWithRed:60/255.0 green:131/255.0 blue:255/255.0 alpha:1.0]];
[navC.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
break;
}
}
}
}
}
结束语
当UINavigationBar完全透明时,也可达到隐藏导航栏效果。