学习iOS自定义导航控制器UINavigationController
自定义导航控制器:将导航控制器中通用的部分拿出来统一设置
1、一般导航条标题的字体setTitleTextAttribute和背景颜色setBackgroundImage都是统一的,可以在load方法中使用appearanceWhenContainedIn统一设置
2、一般导航条的返回按钮需要自定义,一般除了栈底控制器有导航条,其他控制器都需要隐藏底部的条,可以重写pushViewController:animated:方法,在该方法中实现该功能
3、导航控制器右滑返回效果(触摸屏幕的任意一点,向右滑动返回)
UIViewController
--navigationItem
leftBarButtonItem|leftBarButtonItems
NSStringtitle|UIViewtitleView
rightBarButtonItem|rightBarButtonItems
backBarButtonItem
#import"BWNavigationController.h"
#import"UIBarButtonItem+Item.h"
@interfaceBaseNavigationController()<UIGestureRecognizerDelegate>
@end
@implementationBWNavigationController
+(void)load{
[superload];
UINavigationBar*navigationBar=[UINavigationBarappearanceWhenContainedIn:self,nil];
NSMutableDictionary*dict=[NSMutableDictionarydictionary];
dict[NSFontAttributeName]=[UIFontboldSystemFontOfSize:20];
[navigationBarsetTitleTextAttributes:dict];
[navigationBarsetBackgroundImage:[UIImageimageNamed:@"navigationbarBackgroundWhite"]forBarMetrics:UIBarMetricsDefault];
}
-(void)viewDidLoad{
[superviewDidLoad];
//屏幕边缘滑动(只能在屏幕的边缘才能触发该手势,不能在屏幕的任意一点触发该手势)
UIScreenEdgePanGestureRecognizer*edgePanGestureRecognizer=(UIScreenEdgePanGestureRecognizer*)self.interactivePopGestureRecognizer;
//滑动手势(禁用系统自带的屏幕边缘滑动手势,使用自定义的滑动手势目的就是达到触摸屏幕上的任意一点向右滑动都能实现返回的效果)
UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]initWithTarget:edgePanGestureRecognizer.delegateaction:@selector(handleNavigationTransition:)];
panGestureRecognizer.delegate=self;
[self.viewaddGestureRecognizer:panGestureRecognizer];
//禁用系统的屏幕边缘滑动手势
edgePanGestureRecognizer.enabled=NO;
}
//是否允许触发手势,如果是根视图控制器则不需要
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldReceiveTouch:(UITouch*)touch{
returnself.childViewControllers.count>1;
}
-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated{
//统一设置返回按钮
NSIntegercount=self.childViewControllers.count;
if(count>0){
viewController.hidesBottomBarWhenPushed=YES;
viewController.navigationItem.leftBarButtonItem=[UIBarButtonItembackBarButtonItemWithImage:[UIImageimageNamed:@"navigationButtonReturn"]highlightImage:[UIImageimageNamed:@"navigationButtonReturnClick"]tagert:selfaction:@selector(back)title:@"返回"];
}
[superpushViewController:viewControlleranimated:animated];
}
-(void)back{
[selfpopViewControllerAnimated:YES];
}
@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。