-
禁止側(cè)滑初步實現(xiàn)
以前寫過博客導航控制器全屏滑動返回效果
但是現(xiàn)在我的項目都是使用第三方RTRootNavigationController來實現(xiàn)导饲,但是使用RTRootNavigationController如何禁止側(cè)滑呢?
- (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之后的控制器通過上面的方法就沒法控制側(cè)滑了
-
禁止側(cè)滑終結版
了解導航控制器全屏滑動返回效果知道側(cè)滑的原理之后可以用如下方法進行控制了,萬能的方法背零。
在需要禁止的控制器添加代理<UIGestureRecognizerDelegate>
腰吟,并添加如下代碼
- (void)viewDidLoad {
[super viewDidLoad];
// 禁止側(cè)滑
id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
[self.view addGestureRecognizer:pan];
}