Files
keyboard/keyBoard/Class/Base/VC/BaseNavigationController.m
2025-12-18 14:57:28 +08:00

98 lines
3.6 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// BaseNavigationController.m
// keyBoard
//
// Created by Mac on 2025/10/29.
//
#import "BaseNavigationController.h"
@interface BaseNavigationController () <UIGestureRecognizerDelegate>
@end
@implementation BaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// 统一返回箭头样式与颜色
UIImage *backImg = [UIImage imageNamed:@"back_black_icon"];
if (backImg) {
self.navigationBar.backIndicatorImage = backImg;
self.navigationBar.backIndicatorTransitionMaskImage = backImg;
}
self.navigationBar.tintColor = [UIColor blackColor]; // 箭头/按钮的着色
if (@available(iOS 14.0, *)) {
self.navigationBar.topItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
}
// 统一开启“边缘右滑返回”interactivePopGestureRecognizer 在自定义导航栏/隐藏系统导航栏场景下可能失效)
if (self.interactivePopGestureRecognizer) {
self.interactivePopGestureRecognizer.delegate = self;
self.interactivePopGestureRecognizer.enabled = YES;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = true;
UIViewController *prev = self.topViewController;
if (@available(iOS 14.0, *)) {
prev.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
} else {
prev.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
}
[super pushViewController:viewController animated:animated];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return self.viewControllers.count > 1;
}
return YES;
}
/// Push 一个 VC若栈中已存在相同 class 的 VC则先移除旧的再 push 新的。
- (void)kb_pushViewControllerRemovingSameClass:(UIViewController *)viewController
animated:(BOOL)animated {
if (!viewController) { return; }
NSMutableArray<UIViewController *> *stack = self.viewControllers.mutableCopy;
// 移除同 class 的旧 VC通常只有一个这里按需移除第一个
UIViewController *toRemove = nil;
for (UIViewController *vc in stack) {
if ([vc isKindOfClass:[viewController class]]) {
toRemove = vc;
break;
}
}
if (toRemove) {
[stack removeObject:toRemove];
}
// 追加新 VC
[stack addObject:viewController];
// 对前一个 VC 做统一的返回按钮样式处理,与 pushViewController: 保持一致
if (stack.count > 1) {
viewController.hidesBottomBarWhenPushed = YES;
UIViewController *prev = stack[stack.count - 2];
if (@available(iOS 14.0, *)) {
prev.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeMinimal;
} else {
prev.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
}
[self setViewControllers:stack animated:animated];
}
@end