之前在網上找的側滑全屏側滑返回,發現會與頁面其他手勢沖突,自己做了一下修改,發現還是挺好用的,分享給大家。
這是自定義的NavigationController
#import "Navigation.h"
@interface Navigation ()
@property (nonatomic,strong)UIPanGestureRecognizer *pan;
@property (nonatomic,strong)UIScreenEdgePanGestureRecognizer *screenEdgeGesOut;
@end
@implementation Navigation
+(void)initialize{
? ? //要用appearance初始化
? ? UINavigationBar *bar = [UINavigationBar appearance];
//? ? [bar setBarStyle:UIBarStyleBlack];
//? ? [bar setBackgroundColor:[UIColor whiteColor]];
//? ? [bar setBackgroundImage:[UIImage imageNamed:@"背景"] forBarMetrics:UIBarMetricsDefault];
? ? [barsetBarTintColor:[UIColor whiteColor]];//UIColorFromRGB(0xff386b)
? ? bar.translucent=NO;
? ? NSMutableDictionary *dic = [NSMutableDictionary dictionary];
? ? dic[NSForegroundColorAttributeName] = [UIColor colorWithRed:62/255.f green:62/255.f blue:62/255.f alpha:1];//[UIColor whiteColor]
? ? dic[NSFontAttributeName] = [UIFont systemFontOfSize:15 weight:0.3];// [UIFont fontWithName:@"HiraKakuProN-W6" size:15];
? ? [barsetTitleTextAttributes:dic];
? ? bar.barStyle = UIBarStyleDefault;// UIBarStyleBlack
//? ? //設置item屬性
//? ? UIBarButtonItem *item = [UIBarButtonItem appearance];
//? ? NSMutableDictionary *itemArrays = [NSMutableDictionary dictionary];
//? ? itemArrays[NSFontAttributeName] = [UIFont systemFontOfSize:16];
//? ? itemArrays[NSForegroundColorAttributeName] = UIColorFromRGB(0x323232);
//? ? [item setTitleTextAttributes:itemArrays forState:UIControlStateNormal];
//? ?
//? ? NSMutableDictionary *itemDisAttrys = [NSMutableDictionary dictionary];
//? ? itemDisAttrys[NSForegroundColorAttributeName] = [UIColor whiteColor];
//? ? [item setTitleTextAttributes:itemDisAttrys forState:UIControlStateDisabled];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
? ? return (self.topViewController !=[self.viewControllers firstObject]);
}
//? 防止導航控制器只有一個rootViewcontroller時觸發手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
? ? //解決與左滑手勢沖突
? ? NSLog(@"%lu,%@",(unsigned long)self.childViewControllers.count,gestureRecognizer.view);
//如果哪個控制器不需要策劃返回,就可以返回NO
//? ? if ([self.topViewController isKindOfClass:[CustomerServiceController class]]){
//? ? ? ? return NO;
//? ? }else if ([self.topViewController isKindOfClass:[ChatViewController class]]){
//? ? ? ? return NO;
//? ? }else if ([self.topViewController isKindOfClass:[WJLoginSelectViewController class]]){
//? ? ? ? return NO;
//? ? }
? ? return self.childViewControllers.count == 1 ? NO : YES;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? id target = self.interactivePopGestureRecognizer.delegate;
? ? SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
? ? self.screenEdgeGesOut = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:target action:handler];
? ? self.screenEdgeGesOut.edges = UIRectEdgeLeft;
? ? self.screenEdgeGesOut.delegate = self;
? ? UIView *targetView = self.interactivePopGestureRecognizer.view;
? ? [targetViewaddGestureRecognizer:self.screenEdgeGesOut];
? ? // 關閉邊緣觸發手勢 防止和原有邊緣手勢沖突
? ? [self.interactivePopGestureRecognizer setEnabled:NO];
}
- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
{
? ? if (self.childViewControllers.count !=0) {
? ? ? ? //設置返回按鈕
? ? ? ? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
? ? ? ? [buttonaddTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? //尺寸自適應
? ? ? ? button.frame=CGRectMake(0,0,12,18);
? ? ? ? // 讓按鈕內部的所有內容左對齊
? ? ? ? button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
? ? ? ? //設置文字顏色
? ? ? ? [buttonsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
? ? ? ? [buttonsetTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
? ? ? ? button.titleLabel.font = [UIFont systemFontOfSize:18];
//? ? ? ? //設置箭頭圖片
//? ? ? ? [button setImage:[UIImage imageNamed:@"back"]forState:UIControlStateNormal];
//? ? ? ? [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
? ? ? ? //尺寸自適應
//? ? ? ? button.JY_size= CGSizeMake(kWidth(10), kHeight(17));
? ? ? ? //設置箭頭圖片
? ? ? ? [buttonsetImage:[UIImage imageNamed:@"back"]forState:UIControlStateNormal];
? ? ? ? [buttonsetImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
? ? ? ? viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
? ? ? ? // 隱藏tabbar
? ? ? ? viewController.hidesBottomBarWhenPushed=YES;
? ? }
? ? //放在后面,可以覆蓋上面設置的leftBarButtonItem
? ? [superpushViewController:viewControlleranimated:animated];
}
//返回上一頁
- (void)back
{
? ? [self popViewControllerAnimated:YES];
}