iOS导航栏整体滑动解决方案(类似淘宝)

效果图

原生

original1.gif

original2.gif

现在

pure1.gif

pure2.gif

思路

UINavigationController的view视图结构:

-- UIView: frame = (0 0; 414 896);
    -- UINavigationTransitionView: frame = (0 0; 414 896);
    -- UINavigationBar: frame = (0 44; 414 44);

UINavigationTransitionViewUINavigationController 转场动画的容器视图。

UINavigationController 的所有子视图控制器 共用一个UINavigationbarUINavigationbar 的转场效果由参与转场的两个视图控制器对导航栏的设置决定。

UINavigationbar 截图添加在 UIViewcontroller 上并隐藏UINavigationbar 可做到整体转场的效果。

UINavigationControllerview 调用 sendSubviewToBack:UINavigationbar隐藏起来。

转场完成为避免有其他未知影响需调用 bringSubviewToFront:还原UINavigationbar的位置。

UIView 的截图由自身的属性设置和 bounds 大小决定。UINavigationController的根视图控制器没有转场效果,因此延迟截图到 viewDidAppear: ,可以保证此时导航栏截图的大小是正确的。

为了让该功能有全局效果,且简单易用,不需要额外的继承关系,可以使用 runtime 的方法交换对UIViewController的生命周期做功能的扩展。

Method-Swizzle 详细参考

为UIView新增一个分类方法实现截图

- (UIImage *)snapshotImage
{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snap;
}

扩展 viewWillLayoutSubviews :

- (void)yr_viewWillLayoutSubviews
{
    [self yr_viewWillLayoutSubviews];
    
    if (!self.navigationController || self.navigationBarSnapshotView) {
        return;
    }
    
    UIViewController *rootViewController = self.navigationController.viewControllers.firstObject;
    if (self != rootViewController &&
        [self shouldGenerateNavigationBarImageView]) {
        UIImageView *navigationBarSnapshot = [[UIImageView alloc] initWithFrame:self.navigationController.navigationBar.visibleBoundry];
        navigationBarSnapshot.image = [self.navigationController.navigationBar snapshotImageClipsToBounds:NO];
        self.navigationBarSnapshotView = navigationBarSnapshot;
    }
    
    if (self.navigationBarSnapshotView &&
        !self.navigationBarSnapshotView.superview) {
        [self.view addSubview:self.navigationBarSnapshotView];
    }
}

根视图控制器不需要转场效果且为了得到正确的bounds大小,延迟截图到 viewDidAppear: :

- (void)yr_viewDidAppear:(BOOL)animated
{
    [self yr_viewDidAppear:animated];
    
    if (!self.navigationController) {
        return;
    }
    
    if ([self shouldGenerateNavigationBarImageView] &&
        !self.navigationBarSnapshotView) {
        UIImageView *navigationBarSnapshot = [[UIImageView alloc] initWithFrame:self.navigationController.navigationBar.visibleBoundry];
        navigationBarSnapshot.image = [self.navigationController.navigationBar snapshotImageClipsToBounds:NO];
        self.navigationBarSnapshotView = navigationBarSnapshot;
    }
        
    if (self.navigationBarSnapshotView.superview) {
        [self.navigationBarSnapshotView removeFromSuperview];
    }
  
    [self.navigationController.view bringSubviewToFront:self.navigationController.navigationBar];
}

Run起来看一下效果。

wrong.gif

效果不正确,侧滑返回的时候,截图上面有一块留白。

查看UINavigationbar的结构:

UINavigationbar: frame = (0 44; 414 44);
    _UIBarBackground: frame = (0 -44; 414 88);
    _UINavigationBarContentView: frame = (0 0; 414 44);
    UIView: frame = (0 0; 0 0); 

我们设置的 barTintColor 会作用在 _UIBarBackground

可以看到UINavigationbarframe是在状态栏之下,但_UIBarBackground的高度包含了导航栏与状态栏,超出了UINavigationbarframe

CALayerrenderInContext: 只会绘制自身 size 大小的内容。

现在要解决的问题是绘制UIView时要包含超出自身 size 的内容。

UIView扩展一个属性 visibleBoundry 返回视图的可视frame(包含子视图超出自身frame的内容)。

如图视图A包含一个子视图B,视图B的内容超出了A,视图A的 visibleBoundry 就为虚线框起来的大小。

递归遍历视图的 subviews 得到子视图相对自身父视图的 frame 并取两个frame的合集就就得到 visibleBoundry

- (void)_caculateVisibleBoundry:(UIView *)view
{
    if (view.subviews.count == 0) {
        CGRect subRect = [self.superview convertRect:view.bounds fromView:view];
        self.p_visibleBoundry = CGRectUnion(self.p_visibleBoundry, subRect);
    }
    for (UIView *subView in view.subviews) {
        [self _caculateVisibleBoundry:subView];
    }
}

得到 visibleBoundry 后由于绘制区域大小的变化还需要对绘制坐标做个平移才能得到正确的绘制内容。
更改截图方法为 - (UIImage *)snapshotImageClipsToBounds:(BOOL)clipsToBounds

clipsToBoundsYES绘制自身 bounds 大小的内容,clipsToBoundsNO保留超出自身 bounds内容。代码如下:

- (UIImage *)snapshotImageClipsToBounds:(BOOL)clipsToBounds
{
    if (clipsToBounds) {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snap;
    } else {
        UIGraphicsBeginImageContextWithOptions(self.visibleBoundry.size, self.opaque, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        self.p_visibleBoundry = self.frame;
        [self _caculateVisibleBoundry:self];
        CGContextTranslateCTM(context, self.frame.origin.x - self.p_visibleBoundry.origin.x, self.frame.origin.y - self.p_visibleBoundry.origin.y);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snap;
    }
}

导航栏隐藏后系统侧滑返回手势将不可用,为UINavigationController添加 UIPanGestureRecognizer来处理侧滑,代码如下:

最终效果:

pure1

iOS导航栏整体侧滑我已经做成了pod库。可以直接导入使用 YRNavigationBarPure

目前已经有其他iOS导航栏整体侧滑的方案,但看过实现后,这种实现是最轻量级的,无需继承,处理逻辑比较简单,不容易出现bug。

YRNavigationBarPureUIViewControllerUINavigationController新增了扩展属性。通过 runtime做到了无感知,无需继承,使用方便。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270