自定义可以侧滑返回的navigation

本文介绍如何通过继承 UINavigationController 来实现自定义导航控制器,并设置了导航栏的背景颜色、透明度及返回按钮样式等,确保了良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "NDNavigationController.h"


@interface NDNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property(nonatomic, weak) UIViewController *currentShowVC;

@end


@implementation NDNavigationController


- (void)viewDidLoad {

    [super viewDidLoad];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

   

}


- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {


    

    if (self = [super initWithRootViewController:rootViewController]) {

        self.delegate = self;

        self.interactivePopGestureRecognizer.delegate = self;

        // 设置navigationBar的背景颜色,根据需要自己设置

        self.navigationBar.barTintColor = kTabBarColor;

        // 设置navigationBar是否透明,不透明的话会使可用界面原点下移(00)点为导航栏左下角下方的那个点

        self.navigationBar.translucent = NO;

        // 设置navigationBar是不是使用系统默认返回,默认为YES

        self.interactivePopGestureRecognizer.enabled = YES;

        // 设置navigationBar元素的背景颜色,不包括title

        self.navigationBar.tintColor = KCommentColor;

        // 设置navigationControllertitle的字体颜色

        NSDictionary * dict=[NSDictionary dictionaryWithObject:kMainBlackColor forKey:NSForegroundColorAttributeName];

        self.navigationBar.titleTextAttributes = dict;

      

        // 统一替换 back item 的图片

        UIImage * image = [UIImage imageNamed:@"com_navigatin_back_bt"];

        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        [self.navigationBar setBackIndicatorImage:image];

        [self.navigationBar setBackIndicatorTransitionMaskImage:image];

    }

    

    return self;

}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (1 == navigationController.viewControllers.count) {

        self.currentShowVC = nil;

    } else {

        self.currentShowVC = viewController;

    }

}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if (gestureRecognizer == self.interactivePopGestureRecognizer) {

        return (self.currentShowVC == self.topViewController);

    }

    return YES;

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&

        [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {

        return YES;

    } else {

        return NO;

    }

}

//解决:手指在滑动的时候,被 pop ViewController 中的 UIScrollView 会跟着一起滚动

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值