首先在Vc的类别中
@interface UIViewController (Cloudox)
@property (copy, nonatomic) NSString *navBarBgAlpha;
@end
#import "UIViewController+Cloudox.h"
#import
#import "UINavigationController+Cloudox.h"
@implementation UIViewController (Cloudox)
//定义常量 必须是C语言字符串
static char *CloudoxKey = "CloudoxKey";
- (void)setNavBarBgAlpha:(NSString *)navBarBgAlpha {
/*
OBJC_ASSOCIATION_ASSIGN; //assign策略
OBJC_ASSOCIATION_COPY_NONATOMIC; //copy策略
OBJC_ASSOCIATION_RETAIN_NONATOMIC; // retain策略
OBJC_ASSOCIATION_RETAIN;
OBJC_ASSOCIATION_COPY;
*/
/*
* id object 给哪个对象的属性赋值
const void *key 属性对应的key
id value 设置属性值为value
objc_AssociationPolicy policy 使用的策略,是一个枚举值,和copy,retain,assign是一样的,手机开发一般都选择NONATOMIC
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
*/
objc_setAssociatedObject(self, CloudoxKey, navBarBgAlpha, OBJC_ASSOCIATION_COPY_NONATOMIC);
// 设置导航栏透明度(利用Category自己添加的方法)
[self.navigationController setNeedsNavigationBackground:[navBarBgAlpha floatValue]];
}
- (NSString *)navBarBgAlpha {
return objc_getAssociatedObject(self, CloudoxKey) ? : @"1.0";
}
@end
#import
@interface UINavigationController (Cloudox)
@property (copy, nonatomic) NSString *cloudox;
- (void)setNeedsNavigationBackground:(CGFloat)alpha;
@end
//
// UINavigationController+Cloudox.m
// SmoothNavDemo
//
// Created by Cloudox on 2017/3/19.
// Copyright © 2017年 Cloudox. All rights reserved.
//
#import "UINavigationController+Cloudox.h"
#import
#import "UIViewController+Cloudox.h"
@implementation UINavigationController (Cloudox)
// 设置导航栏背景透明度
- (void)setNeedsNavigationBackground:(CGFloat)alpha {
// 导航栏背景透明度设置
UIView *barBackgroundView = [[self.navigationBar subviews] objectAtIndex:0];// _UIBarBackground
UIImageView *backgroundImageView = [[barBackgroundView subviews] objectAtIndex:0];// UIImageView
if (self.navigationBar.isTranslucent) {
if (backgroundImageView != nil && backgroundImageView.image != nil) {
barBackgroundView.alpha = alpha;
} else {
UIView *backgroundEffectView = [[barBackgroundView subviews] objectAtIndex:1];// UIVisualEffectView
if (backgroundEffectView != nil) {
backgroundEffectView.alpha = alpha;
}
}
} else {
barBackgroundView.alpha = alpha;
}
// 对导航栏下面那条线做处理
self.navigationBar.clipsToBounds = alpha == 0.0;
}
+ (void)initialize {
if (self == [UINavigationController self]) {
// 交换方法
SEL originalSelector = NSSelectorFromString(@"_updateInteractiveTransition:");
SEL swizzledSelector = NSSelectorFromString(@"et__updateInteractiveTransition:");
Method originalMethod = class_getInstanceMethod([self class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
// 交换的方法,监控滑动手势
- (void)et__updateInteractiveTransition:(CGFloat)percentComplete {
[self et__updateInteractiveTransition:(percentComplete)];
UIViewController *topVC = self.topViewController;
if (topVC != nil) {
id coor = topVC.transitionCoordinator;
if (coor != nil) {
// 随着滑动的过程设置导航栏透明度渐变
CGFloat fromAlpha = [[coor viewControllerForKey:UITransitionContextFromViewControllerKey].navBarBgAlpha floatValue];
CGFloat toAlpha = [[coor viewControllerForKey:UITransitionContextToViewControllerKey].navBarBgAlpha floatValue];
CGFloat nowAlpha = fromAlpha + (toAlpha - fromAlpha) * percentComplete;
NSLog(@"from:%f, to:%f, now:%f",fromAlpha, toAlpha, nowAlpha);
[self setNeedsNavigationBackground:nowAlpha];
}
}
}
#pragma mark - UINavigationController Delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *topVC = self.topViewController;
if (topVC != nil) {
id coor = topVC.transitionCoordinator;
if (coor != nil) {
// [coor notifyWhenInteractionChangesUsingBlock:^(id context){
// [self dealInteractionChanges:context];
// }];
// notifyWhenInteractionChangesUsingBlock是10.0以后的api,换成notifyWhenInteractionEndsUsingBlock
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[coor notifyWhenInteractionEndsUsingBlock:^(id context){
[self dealInteractionChanges:context];
#pragma clang diagnostic pop
}];
}
}
}
- (void)dealInteractionChanges:(id)context {
if ([context isCancelled]) {// 自动取消了返回手势
NSTimeInterval cancelDuration = [context transitionDuration] * (double)[context percentComplete];
[UIView animateWithDuration:cancelDuration animations:^{
CGFloat nowAlpha = [[context viewControllerForKey:UITransitionContextFromViewControllerKey].navBarBgAlpha floatValue];
NSLog(@"自动取消返回到alpha:%f", nowAlpha);
[self setNeedsNavigationBackground:nowAlpha];
}];
} else {// 自动完成了返回手势
NSTimeInterval finishDuration = [context transitionDuration] * (double)(1 - [context percentComplete]);
[UIView animateWithDuration:finishDuration animations:^{
CGFloat nowAlpha = [[context viewControllerForKey:
UITransitionContextToViewControllerKey].navBarBgAlpha floatValue];
NSLog(@"自动完成返回到alpha:%f", nowAlpha);
[self setNeedsNavigationBackground:nowAlpha];
}];
}
}
#pragma mark - UINavigationBar Delegate
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
if (self.viewControllers.count >= navigationBar.items.count) {// 点击返回按钮
UIViewController *popToVC = self.viewControllers[self.viewControllers.count - 1];
[self setNeedsNavigationBackground:[popToVC.navBarBgAlpha floatValue]];
// [self popViewControllerAnimated:YES];
}
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item {
// push到一个新界面
[self setNeedsNavigationBackground:[self.topViewController.navBarBgAlpha floatValue]];
}
//定义常量 必须是C语言字符串
static char *CloudoxKey = "CloudoxKey";
-(void)setCloudox:(NSString *)cloudox{
/*
OBJC_ASSOCIATION_ASSIGN; //assign策略
OBJC_ASSOCIATION_COPY_NONATOMIC; //copy策略
OBJC_ASSOCIATION_RETAIN_NONATOMIC; // retain策略
OBJC_ASSOCIATION_RETAIN;
OBJC_ASSOCIATION_COPY;
*/
/*
* id object 给哪个对象的属性赋值
const void *key 属性对应的key
id value 设置属性值为value
objc_AssociationPolicy policy 使用的策略,是一个枚举值,和copy,retain,assign是一样的,手机开发一般都选择NONATOMIC
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
*/
objc_setAssociatedObject(self, CloudoxKey, cloudox, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSString *)cloudox{
return objc_getAssociatedObject(self, CloudoxKey);
}
@end
这个具体的代码。目的是实现导航栏的透明度功能。
但是在xcode 9上去打印:
[图片上传中...(983D2B96-9079-4262-B5E1-E06A04237389.png-7ce040-1543221002264-0)]
在xcode 10上面我们看到了如下:
目前我还未找到方案,希望看到的大佬踊跃回复。
洁简的Bug第三个
有人提关于导航栏底部黑色线条的通知用如下方式实现:
接下来回去寻找更好的实现关于导航栏透明度的问题。