//
// TabbarViewController.m
// TabbarDemo
//
// Created by Chocolate on 2022/7/27.
//
import "TabbarViewController.h"
import "MainViewController.h"
import "FindViewController.h"
import "StudyViewController.h"
import "MineViewController.h"
define RCColorWithValue(v) [UIColor colorWithRed:(((v) >> 16) & 0xff)/255.0f green:(((v) >> 8) & 0xff)/255.0f blue:((v) & 0xff)/255.0f alpha:1]
define kColorTitle RCColorWithValue(0x333333) // App字体颜色
define kColorTitleHighlight RCColorWithValue(0xFF5C00)
@interface TabbarViewController ()
@end
@implementation TabbarViewController
-
(void)viewDidLoad {
[super viewDidLoad];UIColor *backgroundColor = RCColorWithValue(0xfafafa);
self.tabBar.barTintColor = backgroundColor;
self.tabBar.backgroundColor = backgroundColor;
[self.tabBar setBackgroundImage:[UIImage new]];
[self.tabBar setShadowImage:[UIImage new]];
//
// self.tabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
// self.tabBar.layer.shadowOffset = CGSizeMake(0, -5);
// self.tabBar.layer.shadowOpacity = 0.3;// 五十音
MainViewController *mainVC = [[MainViewController alloc]init];
UINavigationController *mainNav = [[UINavigationController alloc]initWithRootViewController:mainVC];[self setBaseInfo:mainVC title:@"五十音" normalImage:@"main" selectImage:@"main_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];
// 发现
FindViewController *findVC = [[FindViewController alloc]init];
findVC.title = @"发现";
UINavigationController *findNav = [[UINavigationController alloc]initWithRootViewController:findVC];[self setBaseInfo:findVC title:@"发现" normalImage:@"find" selectImage:@"find_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];
// 跟老师学
StudyViewController *learnVC = [[StudyViewController alloc]init];
UINavigationController *learnNav = [[UINavigationController alloc]initWithRootViewController:learnVC];[self setBaseInfo:learnVC title:@"跟老师学" normalImage:@"study" selectImage:@"study_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];
// 我的
MineViewController *mineVC = [[MineViewController alloc]init];
UINavigationController *mineNav = [[UINavigationController alloc]initWithRootViewController:mineVC];[self setBaseInfo:mineVC title:@"我的" normalImage:@"mine" selectImage:@"mine_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];
self.viewControllers = @[mainNav, findNav, learnNav, mineNav];
} -
(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];NSInteger marginLeft = 50;
NSInteger tabHeight = 44;
NSInteger marginBottom = 40;
self.tabBar.layer.cornerRadius = tabHeight/2;
self.tabBar.frame = CGRectMake(marginLeft, self.view.bounds.size.height - tabHeight - marginBottom, self.view.bounds.size.width - 2*marginLeft, tabHeight);
}
/// 设置tab基本信息
/// @param vc controller
/// @param title 标题
/// @param normalImage 默认图
/// @param selectImage 选中图
/// @param normalColor 默认字体颜色
/// @param selectColor 选中字体颜色
-
(void)setBaseInfo:(UIViewController *)vc
title:(NSString *)title
normalImage:(NSString *)normalImage
selectImage:(NSString *)selectImage
normalColor:(UIColor *)normalColor
selectColor:(UIColor *)selectColor {
vc.title = title;UIImage *mainNormal = [UIImage imageNamed:normalImage];
UIImage *mainSelected = [UIImage imageNamed:selectImage];[vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : normalColor} forState:UIControlStateNormal];
if (@available(iOS 13.0, *)) {
self.tabBar.tintColor = selectColor;
}
[vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : selectColor} forState:UIControlStateSelected];vc.tabBarItem.selectedImage = [mainSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.image = [mainNormal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
@end
iphonex push后item变形
//
// UITabBar+FixPush.m
// XXTabBar
//
// Created by xx on 2019/6/19.
// Copyright © 2019 xx. All rights reserved.
//
import "UITabBar+FixPush.h"
import <objc/runtime.h>
CG_INLINE BOOL
OverrideImplementation(Class targetClass, SEL targetSelector, id (^implementationBlock)(Class originClass, SEL originCMD, IMP originIMP)) {
Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
if (!originMethod) {
return NO;
}
IMP originIMP = method_getImplementation(originMethod);
method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
return YES;
}
static CGFloat const kIPhoneXTabbarHeight = 83;
@interface UITabBar ()
@end
@implementation UITabBar (FixPush)
- (BOOL)iPhoneX {
if (@available(iOS 11.0, *)) {
BOOL result = NO;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationUnknown || orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
result = window.safeAreaInsets.top > 0 && window.safeAreaInsets.bottom > 0;
} else {
result = window.safeAreaInsets.bottom > 0 && window.safeAreaInsets.left > 0 && window.safeAreaInsets.right > 0;
}
}
return result;
}
return NO;
}
-
(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
OverrideImplementation(NSClassFromString(@"UITabBar"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
return ^(UIView *selfObject, CGRect firstArgv) {
if ([self iPhoneX]) {
if (firstArgv.size.height != kIPhoneXTabbarHeight) {
firstArgv.size.height = kIPhoneXTabbarHeight;
}
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];CGFloat y =window.bounds.size.height -kIPhoneXTabbarHeight; if (firstArgv.origin.y != y) { firstArgv.origin.y = y; } } // call super void (*originSelectorIMP)(id, SEL, CGRect); originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP; originSelectorIMP(selfObject, originCMD, firstArgv); }; });
});
}
@end
动画
-
(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// Animation
__block NSMutableArray <UIView >tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];NSMutableArray *tabBarBtnArray = [NSMutableArray array];
int tabIndex = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
// 获取UITabBarButton
for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
UIView * tabBarButton = self.tabBar.subviews[i];
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarBtnArray addObject:tabBarButton];
}
}for (UIView *tempView in self.tabBar.subviews) {
if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
for (UIImageView *tempImageView in tempView.subviews) {
if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
[tabBarSwappableImageViews addObject:tempImageView];
}
}
}
}__block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[tabIndex];
[TabAnimationHelper lottieAnimation:currentTabBarSwappableImageView index:tabIndex];
}
/// lottie动画
/// @param currentAnimationView 当前动画视图
/// @param index 当前动画视图索引
-
(void)lottieAnimation:(UIView *)currentAnimationView index:(NSInteger)index {
CGRect frame = currentAnimationView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
//__block LOTAnimationView *lottieAnimationView = [self getAnimationViewAtTabbarIndex:index frame:frame];
__block LOTAnimationView *lottieAnimationView = [LOTAnimationView animationNamed:[NSString stringWithFormat:@"tabbar%ld",index + 1]];
lottieAnimationView.frame = frame;
lottieAnimationView.contentMode = UIViewContentModeScaleAspectFill;
lottieAnimationView.animationSpeed = 1;//self.animationView = animationView;
lottieAnimationView.center = currentAnimationView.center;
[currentAnimationView.superview addSubview:lottieAnimationView];
currentAnimationView.hidden = YES;
[lottieAnimationView playFromProgress:0 toProgress:1 withCompletion:^(BOOL animationFinished) {
currentAnimationView.hidden = NO;
[lottieAnimationView removeFromSuperview];
lottieAnimationView = nil;
}];
}