页面侧滑和tableview的冲突时,我的解决方案
ViewDidload中添加以下代码:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
我是继承UINavigationController不需要进行优化操作体验有需要的可以添加下面的方法;
但是回到navigationController的最顶层的Controller的时候再次侧滑之后,点击某个要push页面的地方,就会造成软件假死,怎么划都没用。
解决办法:
回到navigationController最上层页面的时候实现以下方法:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
即在最上层页面不侧滑返回的时候把的话返回手势关闭掉即可解决这个假死问题,
我的导航
#import "SJFBaseNavViewController.h"
@interface SJFBaseNavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@end
@implementationSJFBaseNavViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
__weak SJFBaseNavViewController *weakSelf = self;
if([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate= weakSelf;
}
}
- (void)navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated {
// 让系统的侧滑返回生效
self.interactivePopGestureRecognizer.enabled = YES;
if(viewController ==self.viewControllers[0]) {
self.interactivePopGestureRecognizer.delegate = self; // 不支持侧滑
}else{
self.interactivePopGestureRecognizer.delegate = nil; // 支持侧滑
}
}
#pragma mark - UIGestureRecognizerDelegate
//这个方法是在手势将要激活前调用:返回YES允许右滑手势的激活,返回NO不允许右滑手势的激活
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
if(gestureRecognizer ==self.interactivePopGestureRecognizer) {
//屏蔽调用rootViewController的滑动返回手势,避免右滑返回手势引起死机问题
if (self.viewControllers.count < 2 ||
self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
returnNO;
}
}
//这里就是非右滑手势调用的方法啦,统一允许激活
return YES;
}
@end
侧滑删除
#pragma mark - 侧滑删除
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath {
// NSLog(@"已经结束编辑cell");
}
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
return YES;
}
- (nullableUISwipeActionsConfiguration*)tableView:(UITableView*)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath*)indexPath API_AVAILABLE(ios(11.0)){
UIContextualAction*deleteRowAction = [UIContextualActioncontextualActionWithStyle:UIContextualActionStyleDestructivetitle:[NSStringstringWithFormat:@"删除"]handler:^(UIContextualAction*_Nonnullaction,__kindofUIView*_NonnullsourceView,void(^_NonnullcompletionHandler)(BOOL)) {
NSLog(@"shanchu");
}];
UIContextualAction*deleteRowAction1 = [UIContextualActioncontextualActionWithStyle:UIContextualActionStyleDestructivetitle:[NSStringstringWithFormat:@"编辑"]handler:^(UIContextualAction*_Nonnullaction,__kindofUIView*_NonnullsourceView,void(^_NonnullcompletionHandler)(BOOL)) {
NSLog(@"编辑");
}];
deleteRowAction.backgroundColor=[UIColorredColor];
deleteRowAction1.backgroundColor=[UIColororangeColor];
// deleteRowAction.accessibilityFrame
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,deleteRowAction1]];
config.performsFirstActionWithFullSwipe = NO;
returnconfig;
}
- (nullableUISwipeActionsConfiguration*)tableView:(UITableView*)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath*)indexPath API_AVAILABLE(ios(11.0)){
return nil;
}
// 在这个代理方法里,可以获取左滑按钮,进而修改其文字颜色,大小等
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath {
for(UIView*subViewintableView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
//这里修改按钮的frame 及 颜色(这里是你要设置成的按钮颜色)
UIView*confirmView=(UIView*)[subView.subviewsfirstObject];
//我的cell高度不固定的
CGFloathh=[self.heightarray[indexPath.row]floatValue];
UIButton*swipeActionStandardBtn = subView.subviews[0];
if([swipeActionStandardBtnisKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
CGFloatswipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
CGFloatswipeActionStandardBtnW = swipeActionStandardBtn.frame.size.width;
swipeActionStandardBtn.frame=CGRectMake(swipeActionStandardBtnOX,8, swipeActionStandardBtnW, hh -12);
}
//2.0修改按钮-颜色
UIButton*swipeActionStandardBtn1 = subView.subviews[1];
if([swipeActionStandardBtn1isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
CGFloatswipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
CGFloatswipeActionStandardBtnW = swipeActionStandardBtn.frame.size.width;
swipeActionStandardBtn1.frame=CGRectMake(swipeActionStandardBtnOX,8, swipeActionStandardBtnW, hh -12);
}
// for (UIView *childView in confirmView.subviews) {
// if ([childView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
// UIButton *button = (UIButton *)childView;
// button.titleLabel.textColor =[UIColor blueColor];
// button.titleLabel.font = [UIFont systemFontOfSize:18];
// }
// }
}
}
}