RRViewControllerExtension
一个轻量级的 UIViewController 分类扩展,用于 UINavigationBar 外观管理、视图控制器推/弹/ dismiss 管理,ViewController 视图统计、内存泄露检测和其他方便的属性和方法。优点包括
- 优雅地管理
UINavigationBar
外观 - 不修改任何代码即可自动检测视图控制器内存泄露。
- 带有完成块回调的推/弹
UIViewController
生命周期方法钩子- ViewController 视图统计
- 其他方便的属性
有关 此演示 的 GitHub 上的参考,中文介绍点击此处。
预览
使用
UINavigationBar
外观管理
使特定的 UINavigationBar
样式针对每个 viewController
静态或动态地设置,只需通过重写你的 viewController
中的方法实现,这些方法在 UIViewController+RRExtension.h
中定义。
//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
样式动态变化,在你的 viewController
的 .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
的内存泄漏,您只需将 RRViewControllerExtension
导入到项目中。当发生内存泄漏时,您的应用将显示一个警告。
您还可以通过在 UIViewController+RRExtension.h
中的方法下引用来指定要执行内存泄漏检测的 UIViewController
类或更精确地说,是指定的 UIViewController
实例。
//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;
<code><vcfbgtgt; <code> 绑定任何 <code>UIViewController<code> 生命周期方法之前或之后执行,例如,如果希望跟踪用户页面浏览行为,只需在您的 <code>AppDelegate.m<code> 中编写代码:
//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 'RRViewControllerExtension'
并且在您的项目文件中导入:
#import <RRViewControllerExtension.h>
或者,从此代码项目将 RRViewControllerExtension 目录拖放到您的 Xcode 项目中,通过以下方式导入文件:
#import "RRViewControllerExtension.h"
待办事项
修复问题:通过:隐藏复位栈后重置导航栏返回箭头: -[UINavigationController setViewControllers:]
作者
罗亮富 (Roen), [email protected]
许可协议
所有源代码均遵循MIT许可证