4
This commit is contained in:
@@ -41,4 +41,42 @@
|
||||
[super pushViewController:viewController animated:animated];
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
Reference in New Issue
Block a user