UI

RRViewControllerExtension,优雅地管理U

2018-09-23  本文已影响351人  贝斯特o罗

RRViewControllerExtension是我在很久之前写的一个轻量级的UIViewController分类,并且已经在户外助手上得到了成熟的应用。
开发者只需要在Xcode工程中加入'RRViewControllerExtension'的代码,甚至都不需要#import任何头文件,就可以轻松地实现不同的UIViewController的导航栏UINavigationBar的“独立”管理,包括导航栏显示/隐藏、设置背景色、背景图片、背景透明、title颜色字体以及UINavigationItem颜色。
另外 RRViewControllerExtension还可以准确地实时监测每一个UIViewController是否存在内存泄漏。

其主要功能包括:

github 演示demo

预览

rrvc003.gif

安装

  1. CocoaPods安装
    在你工程文件的Podfile中加入
pod 'RRViewControllerExtension'
  1. 直接下载源码
    你还可以从这里 下载源码并将RRViewControllerExtension文件夹拖入你的xcode工程文件

注意:一些特定的功能使用需要引入头文件#import <RRViewControllerExtension.h>或者#import "RRViewControllerExtension.h"取决于你是源码导入还是cocopods导入。

使用

定制导航栏 UINavigationBar在不同UIViewController不同的显示样式 ,你只需要在vc的.m文件中实现以下方法(在UIViewController+RRExtension.h中定义的)中的任何一个或多个即可

//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance
//当前界面导航栏是否隐藏
-(BOOL)prefersNavigationBarHidden;
//当前界面导航栏是否设置为透明(只是导航栏透明,navigationItem依然显示)
-(BOOL)prefersNavigationBarTransparent;
//导航栏背景色
-(nullable UIColor *)preferredNavatationBarColor;
//navigationItem颜色
-(nullable UIColor *)preferredNavigationItemColor;
//导航栏背景图片
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
//title显示样式字典
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;

在大部分情况下,我们并不需要去设置每一个viewcontroller的导航栏显示样式,只需让它们显示默认样式即可,设置默认样式有两种方式:

  1. 通过UINavigationBar appearance 设置
    UINavigationBar遵循UIAppearance协议,可以通过 [[UINavigationBar appearance] setXXX:] 设定导航栏UINavigationBar的全局默认显示样式,这种设置是全局性的,也就是说在你的app中所有的导航栏都采用这种默认显示样式。
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0 green:0.45 blue:0.8 alpha:1.0]];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
NSDictionary * dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:dict];
  1. 通过RRViewControllerExtension设置
    通过RRViewControllerExtensionUINavigationController+RRSet中定义的属性,设定每一个导航控制器导航栏UINavigationBar默认显示样式,这种设定针对的是每一个导航控制器实例对象,此时需要引入头文件RRViewControllerExtension.h
// set default navigation bar appearance
@property (nonatomic) BOOL defaultNavigationBarHidden;
@property (nonatomic) BOOL defaultNavigationBarTransparent;

@property (nonatomic,copy) UIColor *defaultNavatationBarColor;
@property (nonatomic,copy) UIColor *defaultNavigationItemColor;
@property (nonatomic,strong) UIImage *defaultNavigationBarBackgroundImage;
@property (nonatomic,copy) NSDictionary *defaultNavigationTitleTextAttributes;

动态修改导航栏显示

坑爹的产品经常会给我们出这样的需求,根据scrollView的滚动高度,来隐藏/显示导航栏,或者把导航栏设置成透明样式,改变导航栏按钮颜色等待,凡此种种操作,现在都能轻松应对;程序员只要在对应的方法中,根据不同的状态返回不同的值,然后再达到触发条件的时候,调用

 [self updateNavigationAppearance:YES];

举个栗子:


    //typically in your UIScrollViewDelegate method
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        BOOL mode;
        if(scrollView.contentOffset.y > 300)
            mode = NO;
        else
            mode = YES;

        if(mode != _previewMode)
        {
            _previewMode = mode;

            //force navigation appearance update
            [self updateNavigationAppearance:YES];
        }
    }
    
    -(BOOL)prefersNavigationBarTransparent
    {
        if(_previewMode)
            return NO;
        else
            return YES;
    }
    
    -(nullable UIColor *)preferredNavigationItemColor
    {
        if(_previewMode)
            return [UIColor whiteColor];
        else
            return [UIColor blackColor];;
    }

智能内存泄漏检测

该功能默认只在debug模式下开启

只需要将RRViewControllerExtension加入到你的xcode工程当中,就可以实现viewcontroller内存泄漏的检测,一旦viewcontroller在消失后内存空间没有被释放,在你的app当中就会弹出内存泄漏警告提醒,如下图

内存泄漏监测.png

某些特定的viewcontroller我们并不希望它在消失后马上销毁,而是可以留着复用,比如微信的朋友圈就这样做的(我的必问面试题),这种情况我们并不希望将其认定为内存泄漏,RRViewControllerExtension提供的方法可以指定某一“个”viewcontroller实例或者某一“类”viewcontroller不参与内存泄漏监测

//Unavailable in release mode. in debug mode, defalut is NO for classes returned from +memoryLeakDetectionExcludedClasses method and YES for others
//当前viewController是否开启内存泄漏检测
@property (nonatomic,getter = memoryLeakDetectionEnabled) BOOL enabledMemoryLeakDetection;

//read and add or remove values from the returned set to change default excluded memory detection classes
//将你不希望参与内存泄漏检测的类添加到这个读取的set里面
+(NSMutableSet<NSString *> *)memoryLeakDetectionExcludedClasses;

//for subclass to override
//当发生内存泄漏时收到消息方法回调,子类可以重写
-(void)didReceiveMemoryLeakWarning;

上一篇下一篇

猜你喜欢

热点阅读