处理tabbar

This commit is contained in:
2026-01-19 19:14:20 +08:00
parent 063ceae10f
commit d1d47336c2
7 changed files with 365 additions and 218 deletions

View File

@@ -14,84 +14,97 @@
@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]; // /
[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;
}
if (@available(iOS 14.0, *)) {
self.navigationBar.topItem.backButtonDisplayMode =
UINavigationItemBackButtonDisplayModeMinimal;
}
// interactivePopGestureRecognizer /
if (self.interactivePopGestureRecognizer) {
self.interactivePopGestureRecognizer.delegate = self;
self.interactivePopGestureRecognizer.enabled = YES;
}
// 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];
}
- (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];
}
[super pushViewController:viewController animated:animated];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return self.viewControllers.count > 1;
}
return YES;
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; }
- (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;
}
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];
}
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];
}
}
// 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];
[self setViewControllers:stack animated:animated];
}
@end

View File

@@ -15,13 +15,12 @@
#import "BaseNavigationController.h"
#import "KBAuthManager.h"
@interface BaseTabBarController ()
#import "KBCustomTabBar.h"
#import "Masonry.h"
@interface BaseTabBarController () <KBCustomTabBarDelegate>
/// TabBar appearance
@property(nonatomic, strong)
UITabBarAppearance *originalAppearance API_AVAILABLE(ios(13.0));
@property(nonatomic, strong)
UITabBarAppearance *transparentAppearance API_AVAILABLE(ios(13.0));
/// TabBar
@property(nonatomic, strong) KBCustomTabBar *customTabBar;
@end
@@ -32,8 +31,6 @@
self.delegate = self;
[self setupTabbarAppearance];
// 4 Tab / / /
// Assets.xcassets/Tabbar tab_home/tab_home_selected
@@ -70,7 +67,10 @@
image:@"tab_my"
selectedImg:@"tab_my_selected"];
self.viewControllers = @[ navHome, navShop, aiMainVC, navMy ];
self.viewControllers = @[ navHome, navShop, navCommunity, navMy ];
// TabBar TabBar
[self setupCustomTabBarOnNativeTabBar];
// Token
// [[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil
@@ -79,66 +79,60 @@
// [[KBAuthManager shared] signOut];
}
- (void)setupTabbarAppearance {
// TabBar view TabBar
self.tabBar.translucent = NO;
if (@available(iOS 15.0, *)) {
UITabBarAppearance *a = [UITabBarAppearance new];
[a configureWithOpaqueBackground];
a.backgroundColor = [UIColor whiteColor];
// iOS 15+ appearance
NSDictionary *selAttr =
@{NSForegroundColorAttributeName : [UIColor blackColor]};
a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
self.tabBar.standardAppearance = a;
self.tabBar.scrollEdgeAppearance = a;
- (void)setupCustomTabBarOnNativeTabBar {
// 1. TabBar
[self hideNativeTabBarButtons];
// appearance
self.originalAppearance = a;
// appearance
UITabBarAppearance *transparentA = [UITabBarAppearance new];
[transparentA configureWithTransparentBackground];
transparentA.backgroundColor = [UIColor clearColor];
transparentA.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
transparentA.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
transparentA.compactInlineLayoutAppearance.selected.titleTextAttributes =
selAttr;
self.transparentAppearance = transparentA;
} else if (@available(iOS 13.0, *)) {
UITabBarAppearance *a = [UITabBarAppearance new];
[a configureWithOpaqueBackground];
a.backgroundColor = [UIColor whiteColor];
NSDictionary *selAttr =
@{NSForegroundColorAttributeName : [UIColor blackColor]};
a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
self.tabBar.standardAppearance = a;
self.tabBar.tintColor = [UIColor blackColor];
// appearance
self.originalAppearance = a;
// appearance
UITabBarAppearance *transparentA = [UITabBarAppearance new];
[transparentA configureWithTransparentBackground];
transparentA.backgroundColor = [UIColor clearColor];
transparentA.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
transparentA.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
transparentA.compactInlineLayoutAppearance.selected.titleTextAttributes =
selAttr;
self.transparentAppearance = transparentA;
} else {
// tintColor/appearance 退
self.tabBar.tintColor = [UIColor blackColor];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor blackColor]
}
forState:UIControlStateSelected];
// 2. TabBar
NSMutableArray *items = [NSMutableArray array];
for (UIViewController *vc in self.viewControllers) {
[items addObject:vc.tabBarItem];
}
self.customTabBar = [[KBCustomTabBar alloc] initWithItems:items];
self.customTabBar.delegate = self;
// 3. frame TabBar
self.customTabBar.frame = self.tabBar.bounds;
// 4. 使 autoresizingMask
self.customTabBar.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// 5. TabBar TabBar
[self.tabBar addSubview:self.customTabBar];
// 6. TabBar
self.tabBar.userInteractionEnabled = YES;
for (UIView *subview in self.tabBar.subviews) {
if (subview != self.customTabBar &&
![NSStringFromClass([subview class]) containsString:@"Background"]) {
subview.userInteractionEnabled = NO;
subview.hidden = YES;
}
}
// 7. 0
[self.customTabBar setTransparentBackground:NO];
}
- (void)hideNativeTabBarButtons {
// TabBar
for (UIView *subview in self.tabBar.subviews) {
// _UIBarBackground
if (![NSStringFromClass([subview class]) containsString:@"Background"] &&
subview != self.customTabBar) {
subview.hidden = YES;
subview.alpha = 0;
}
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//
[self hideNativeTabBarButtons];
}
// TabBarItem Tint
@@ -155,59 +149,24 @@
return item;
}
#pragma mark - UITabBarControllerDelegate
#pragma mark - KBCustomTabBarDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
// tab
if (tabBarController.selectedViewController == viewController) {
return YES;
}
NSUInteger index =
[tabBarController.viewControllers indexOfObject:viewController];
if (index == NSNotFound) {
return YES;
}
// tab 4 3
- (void)customTabBar:(KBCustomTabBar *)tabBar
didSelectItemAtIndex:(NSInteger)index {
//
if ((index == 1 || index == 2) && ![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return NO;
return;
}
return YES;
}
// VC
self.selectedIndex = index;
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSUInteger index =
[tabBarController.viewControllers indexOfObject:viewController];
// tab2 KBAiMainVC
// TabBar 2 KBAiMainVC
if (index == 2) {
if (@available(iOS 13.0, *)) {
self.tabBar.standardAppearance = self.transparentAppearance;
if (@available(iOS 15.0, *)) {
self.tabBar.scrollEdgeAppearance = self.transparentAppearance;
}
} else {
// iOS 13
self.tabBar.barTintColor = [UIColor clearColor];
self.tabBar.backgroundColor = [UIColor clearColor];
}
[self.customTabBar setTransparentBackground:YES];
} else {
// tab
if (@available(iOS 13.0, *)) {
self.tabBar.standardAppearance = self.originalAppearance;
if (@available(iOS 15.0, *)) {
self.tabBar.scrollEdgeAppearance = self.originalAppearance;
}
} else {
// iOS 13
self.tabBar.barTintColor = [UIColor whiteColor];
self.tabBar.backgroundColor = [UIColor whiteColor];
}
[self.customTabBar setTransparentBackground:NO];
}
}