学习iOS自定义导航控制器UINavigationController

自定义导航控制器: 将导航控制器中通用的部分拿出来统一设置

1、一般导航条标题的字体setTitleTextAttribute和背景颜色setBackgroundImage都是统一的,可以在load方法中使用appearanceWhenContainedIn统一设置

2、一般导航条的返回按钮需要自定义,一般除了栈底控制器有导航条,其他控制器都需要隐藏底部的条,可以重写pushViewController:animated:方法,在该方法中实现该功能

3、导航控制器右滑返回效果(触摸屏幕的任意一点,向右滑动返回)

UIViewController 

 --navigationItem 

   leftBarButtonItem | leftBarButtonItems

   NSString title | UIView titleView

   rightBarButtonItem | rightBarButtonItems

   backBarButtonItem

#import "BWNavigationController.h"

#import "UIBarButtonItem+Item.h"

@interface BaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation BWNavigationController

+ (void)load {

[super load];

UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];

[navigationBar setTitleTextAttributes:dict];

[navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];

}

- (void)viewDidLoad {

[super viewDidLoad];

// 屏幕边缘滑动(只能在屏幕的边缘才能触发该手势,不能在屏幕的任意一点触发该手势)

UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;

// 滑动手势(禁用系统自带的屏幕边缘滑动手势,使用自定义的滑动手势目的就是达到触摸屏幕上的任意一点向右滑动都能实现返回的效果)

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:edgePanGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];

panGestureRecognizer.delegate = self;

[self.view addGestureRecognizer:panGestureRecognizer];

// 禁用系统的屏幕边缘滑动手势

edgePanGestureRecognizer.enabled = NO;

}

// 是否允许触发手势,如果是根视图控制器则不需要

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

return self.childViewControllers.count > 1;

}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

// 统一设置返回按钮

NSInteger count = self.childViewControllers.count;

if (count > 0) {

viewController.hidesBottomBarWhenPushed = YES;

viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backBarButtonItemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highlightImage:[UIImage imageNamed:@"navigationButtonReturnClick"] tagert:self action:@selector(back) title:@"返回"];

}

[super pushViewController:viewController animated:animated];

}

- (void)back {

[self popViewControllerAnimated:YES];

}

@end

以上是 学习iOS自定义导航控制器UINavigationController 的全部内容, 来源链接: utcz.com/z/314477.html

回到顶部