RRUIViewControllerExtension
一个轻量级的 UIViewController 分类扩展,用于 UINavigationBar 外观管理、视图控制器推送/弹出/解散管理、内存泄露检测等方便的属性和方法。优点包括
- 优雅地管理
UINavigationBar
外观 - 无需任何代码修改即可自动检测视图控制器内存泄露。
- 使用完成块回调块进行推送/弹出
UIViewController
生命周期方法钩子- 其他方便的属性
参考 github 上的这个示例
用法
UINavigationBar
外观管理
通过覆盖你自己视图控制器中定义的方法(在 UIViewController+RRExtension.h
中定义)来静态或动态地设置每个视图控制器的特定 UINavigationBar
栏的外观
//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance
-(BOOL)prefersNavigationBarHidden;
-(BOOL)prefersNavigationBarTransparent;
-(nullable UIColor *)preferredNavatationBarColor;
-(nullable UIColor *)preferredNavigationItemColor;
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;
要使 UINavigationBar
栏的外观动态更改,在视图控制器的 .m 文件中调用 [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];;
}
您可以使用 [[UINavigationBar appearance] setXXX:]
全局指定默认的 UINavigationBar
外观。
[[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];
您还可以通过设置在 UINavigationController+RRSet.h
中定义的属性为每个 UINavigationController
实例指定默认的 UINavigationBar
外观。
// 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;
内存泄漏检测
要检测运行时viewcontroller的内存泄漏,只需将RRUIViewControllerExtension
导入到项目中。每次发生内存泄漏时,应用程序上都会显示一个警告框。
您还可以指定要检测内存泄漏的UIViewController
类,或者更确切地说,在哪个UIViewController
实例上执行内存泄漏检测,可以通过查看下面在UIViewController+RRExtension.h
中的方法来实现。
//Unavailable in release mode. \
in debug mode, defalut is NO for classes returned from +memoryLeakDetectionExcludedClasses method and YES for others
@property (nonatomic,getter = memoryLeakDetectionEnabled) BOOL enabledMemoryLeakDetection;
//read and add or remove values from the returned set to change default excluded memory detection classes
+(NSMutableSet<NSString *> *)memoryLeakDetectionExcludedClasses;
//for subclass to override
-(void)didReceiveMemoryLeakWarning;
viewController生命周期挂钩
在执行前后挂钩任何UIViewController
生命周期方法,例如,如果您想跟踪用户页面查看行为,您只需要在您的AppDelgate.m
中编写代码,如下所示
//log the user enter page behavior
[UIViewController hookLifecycle:RRViewControllerLifeCycleViewWillAppear
onTiming:RRMethodInsertTimingBefore
withBlock:^(UIViewController * _Nonnull viewController, BOOL animated) {
[MyLog logEnterPage:NSStringFromClass([viewController class])];
}];
//log the user leaving page behavior
[UIViewController hookLifecycle:RRViewControllerLifeCycleViewDidDisappear
onTiming:RRMethodInsertTimingAfter
withBlock:^(UIViewController * _Nonnull viewController, BOOL animated) {
[MyLog logLeavePage:NSStringFromClass([viewController class])];
}];
安装
要使用CocoaPods安装,请将以下内容添加到项目的Podfile中
pod 'RRUIViewControllerExtension'
然后在您的项目文件中按以下方式导入
#import <RRUIViewControllerExtension.h>
或者,从这个代码项目中将RRUIViewControllerExtension目录拖放到您的Xcode项目中,然后按以下方式导入文件
#import "RRUIViewControllerExtension.h"
作者
Roen, [email protected]
许可证
所有源代码都采用MIT许可证。