-
禁止侧滑初步实现
以前写过博客导航控制器全屏滑动返回效果
但是现在我的项目都是使用第三方RTRootNavigationController来实现,但是使用RTRootNavigationController如何禁止侧滑呢?
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([NSStringFromClass([viewController class]) isEqualToString:@"SCOrderDetailVC"]) {
viewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 0;
self.fd_fullscreenPopGestureRecognizer.enabled = NO;
} else {
viewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = GQ_WindowW;
self.fd_fullscreenPopGestureRecognizer.enabled = YES;
}
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
UIViewController *viewController = [super popViewControllerAnimated:animated];
if ([NSStringFromClass([viewController class]) isEqualToString:@"SCOrderDetailVC"]) {
viewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 0;
self.fd_fullscreenPopGestureRecognizer.enabled = NO;
} else {
viewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = GQ_WindowW;
self.fd_fullscreenPopGestureRecognizer.enabled = YES;
}
return viewController;
}
但是问题来了,一般App在push操作时候会隐藏tabbar,但是如果push过去之后换一个新的tabbar,那么push过去的控制器应该是继承UITabBarController的子类,假如命名SCCrowdFundingTabBarController
-
push换tabbar
SCCrowdFundingTabBarController.h
@interface SCCrowdFundingTabBarController : UITabBarController
@property (nonatomic, strong) SCNavigationController *preNavC;
@end
SCCrowdFundingTabBarController.m
#import "SCCrowdFundingTabBarController.h"
#import "SCCrowdFundingVC.h"
#import "SCTimeMarketSegmentVC.h"
@interface SCCrowdFundingTabBarController ()
@property (nonatomic, strong) SCNavigationController *crowdFundingNavi;
@property (nonatomic, strong) SCCrowdFundingVC *crowdFundingVC;
@property (nonatomic, strong) SCNavigationController *timeMarketNavi;
@property (nonatomic, strong) SCTimeMarketSegmentVC *timeMarketVC;
@end
@implementation SCCrowdFundingTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kLightGrayColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kDeepBlueColor,NSForegroundColorAttributeName, nil]forState:UIControlStateSelected];
self.viewControllers = @[self.crowdFundingNavi,self.timeMarketNavi, self.crowdFundingMeNavi];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
}
- (void)setPreNavC:(SCNavigationController *)preNavC {
_preNavC = preNavC;
self.crowdFundingVC.preNavC = self.preNavC;
self.timeMarketVC.preNavC = self.preNavC;
}
- (SCNavigationController *)crowdFundingNavi {
if (_crowdFundingNavi == nil) {
SCCrowdFundingVC *vc = [[SCCrowdFundingVC alloc]init];
self.crowdFundingVC = vc;
_crowdFundingNavi = [[SCNavigationController alloc] initWithRootViewController:vc];
[self setTabbar:@"众筹" image:@"众筹" seleImage:@"众筹_选中" nav:_crowdFundingNavi];
}
return _crowdFundingNavi;
}
- (SCNavigationController *)timeMarketNavi {
if (_timeMarketNavi == nil) {
SCTimeMarketSegmentVC *vc = [[SCTimeMarketSegmentVC alloc]init];
self.timeMarketVC= vc;
_timeMarketNavi = [[SCNavigationController alloc] initWithRootViewController:vc];
[self setTabbar:@"市场" image:@"时间市场" seleImage:@"时间市场_点击" nav:_timeMarketNavi];
}
return _timeMarketNavi;
}
- (void)setTabbar:(NSString *)title image:(NSString *)image seleImage:(NSString *)seleImage nav:(SCNavigationController *)vc{
vc.tabBarItem.title = title;
vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:seleImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
@end
push时候通过定义preNavC传递之前的NavigationController,保证push的控制器都在同一个navigationController的viewControllers中,这样push、pop操作才生效。
SCCrowdFundingTabBarController *vc = [SCCrowdFundingTabBarController new];
vc.preNavC = (SCNavigationController *)self.navigationController;
[self.navigationController pushViewController:vc animated:YES];
另外,SCCrowdFundingTabBarController里的控制器例如SCCrowdFundingVC也需要接收一个preNavC,通过preNavC来添加返回按钮进行pop后退。
SCCrowdFundingVC.h
@interface SCCrowdFundingVC : GQSegmentViewController
@property (nonatomic, strong) SCNavigationController *preNavC;
@end
SCCrowdFundingVC.m
@implementation SCCrowdFundingVC
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *backButton = [UIButton gq_buttonWithBackButton:@"返回" target:self action:@selector(backButtonClick)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
}
- (void)backButtonClick {
[self.preNavC popViewControllerAnimated:YES];
}
@end
但是进入SCCrowdFundingTabBarController之后的控制器通过上面的方法就没法控制侧滑了
-
禁止侧滑终结版
了解导航控制器全屏滑动返回效果知道侧滑的原理之后可以用如下方法进行控制了,万能的方法。
在需要禁止的控制器添加代理<UIGestureRecognizerDelegate>
,并添加如下代码
- (void)viewDidLoad {
[super viewDidLoad];
// 禁止侧滑
id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
[self.view addGestureRecognizer:pan];
}
网友评论