这篇文章主要为大家详细介绍了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
基础教程推荐
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- IOS获取系统相册中照片的示例代码 2023-01-03
- Android开发Compose集成高德地图实例 2023-06-15
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- iOS开发 全机型适配解决方法 2023-01-14
- iOS开发使用XML解析网络数据 2022-11-12
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- Android实现短信验证码输入框 2023-04-29
- Flutter进阶之实现动画效果(三) 2022-10-28