174 lines
7.8 KiB
Objective-C
174 lines
7.8 KiB
Objective-C
//
|
||
// THTabBarViewController.m
|
||
// TreeHole
|
||
//
|
||
// Created by iOS on 2023/1/31.
|
||
//
|
||
//
|
||
|
||
#import "THTabBarViewController.h"
|
||
#import "THViewController.h"
|
||
#import "THNavigationController.h"
|
||
|
||
@interface THTabBarViewController ()
|
||
|
||
|
||
|
||
@end
|
||
|
||
@implementation THTabBarViewController
|
||
|
||
/*
|
||
- (instancetype)init {
|
||
if (!(self = [super init])) {
|
||
return nil;
|
||
}
|
||
/**
|
||
* 以下两行代码目的在于手动设置让TabBarItem只显示图标,不显示文字,并让图标垂直居中。
|
||
* 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
|
||
* 更推荐后一种做法。
|
||
|
||
UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
|
||
UIOffset titlePositionAdjustment = UIOffsetMake(0, -3.5);
|
||
CYLTabBarController *tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers tabBarItemsAttributes:self.tabBarItemsAttributesForController imageInsets:imageInsets titlePositionAdjustment:titlePositionAdjustment context:nil];
|
||
// tabBarController.tabBarHeight = 49;
|
||
[self customizeTabBarAppearanceWithTitlePositionAdjustment:titlePositionAdjustment];
|
||
// [self customizeTabBarAppearance:tabBarController];
|
||
// self.navigationController.navigationBar.hidden = YES;
|
||
return tabBarController;
|
||
}
|
||
|
||
- (NSArray *)viewControllers {
|
||
THViewController *homeVC = [[THViewController alloc] init];
|
||
homeVC.view.backgroundColor = [UIColor qmui_randomColor];
|
||
UIViewController *homeNav = [[THNavigationController alloc] initWithRootViewController:homeVC];
|
||
// [homeVC cyl_setHideNavigationBarSeparator:YES];
|
||
|
||
THViewController *nearVC = [[THViewController alloc] init];
|
||
nearVC.view.backgroundColor = [UIColor qmui_randomColor];
|
||
UIViewController *nearNav = [[THNavigationController alloc] initWithRootViewController:nearVC];
|
||
// [nearVC cyl_setHideNavigationBarSeparator:YES];
|
||
|
||
NSArray *viewControllers = @[
|
||
homeNav,
|
||
nearNav,
|
||
];
|
||
return viewControllers;
|
||
}
|
||
|
||
- (NSArray *)tabBarItemsAttributesForController {
|
||
NSDictionary *firstTabBarItemsAttributes = @{
|
||
CYLTabBarItemTitle : @"首页",
|
||
CYLTabBarItemImage : @"TH_TabBarBaseLocalNormal",
|
||
CYLTabBarItemSelectedImage : @"TH_TabBarBaseLocalSelected",
|
||
};
|
||
NSDictionary *secondTabBarItemsAttributes = @{
|
||
CYLTabBarItemTitle : @"鱼塘",
|
||
CYLTabBarItemImage :@"TH_TabBarMsgNormal",
|
||
CYLTabBarItemSelectedImage : @"TH_TabBarMsgSelected",
|
||
};
|
||
// NSDictionary *thirdTabBarItemsAttributes = @{
|
||
// CYLTabBarItemTitle : @"我的",
|
||
// CYLTabBarItemImage :@"fishpond_normal",
|
||
// CYLTabBarItemSelectedImage : @"fishpond_highlight",
|
||
// };
|
||
NSArray *tabBarItemsAttributes = @[
|
||
firstTabBarItemsAttributes,
|
||
secondTabBarItemsAttributes,
|
||
// thirdTabBarItemsAttributes
|
||
];
|
||
return tabBarItemsAttributes;
|
||
}
|
||
|
||
/**
|
||
* 更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
|
||
|
||
- (void)customizeTabBarAppearanceWithTitlePositionAdjustment:(UIOffset)titlePositionAdjustment {
|
||
// Customize UITabBar height
|
||
// 自定义 TabBar 高度
|
||
// tabBarController.tabBarHeight = CYL_IS_IPHONE_X ? 65 : 40;
|
||
|
||
[self rootWindow].backgroundColor = [UIColor cyl_systemBackgroundColor];
|
||
|
||
// set the text color for unselected state
|
||
// 普通状态下的文字属性
|
||
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
|
||
normalAttrs[NSForegroundColorAttributeName] = [UIColor cyl_systemGrayColor];
|
||
//normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
|
||
|
||
// set the text color for selected state
|
||
// 选中状态下的文字属性
|
||
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
|
||
selectedAttrs[NSForegroundColorAttributeName] = [UIColor cyl_labelColor];
|
||
//selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
|
||
|
||
// Set the dark color to selected tab (the dimmed background)
|
||
// TabBarItem选中后的背景颜色
|
||
// [self customizeTabBarSelectionIndicatorImage];
|
||
|
||
// update TabBar when TabBarItem width did update
|
||
// If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,
|
||
// remove the comment '//'
|
||
// 如果你的App需要支持横竖屏,请使用该方法移除注释 '//'
|
||
// [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
|
||
|
||
// set background color
|
||
// 设置 TabBar 背景
|
||
// 半透明
|
||
// [UITabBar appearance].translucent = YES;
|
||
// [UITabBar appearance].barTintColor = [UIColor cyl_systemBackgroundColor];
|
||
// [[UITabBar appearance] setBackgroundColor:[UIColor cyl_systemBackgroundColor]];
|
||
|
||
|
||
// [[UITabBar appearance] setBackgroundImage:[[self class] imageWithColor:[UIColor whiteColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, tabBarController.tabBarHeight ?: (CYL_IS_IPHONE_X ? 65 : 40))]];
|
||
// [[UITabBar appearance] setUnselectedItemTintColor:[UIColor systemGrayColor]];
|
||
|
||
//Three way to deal with shadow 三种阴影处理方式:
|
||
// NO.3, without shadow : use -[[CYLTabBarController hideTabBarShadowImageView] in CYLMainRootViewController.m
|
||
|
||
// NO.2,using layer to add shadow.
|
||
// CYLTabBarController *tabBarController = [self cyl_tabBarController];
|
||
// tabBarController.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
|
||
// tabBarController.tabBar.layer.shadowRadius = 15.0;
|
||
// tabBarController.tabBar.layer.shadowOpacity = 1;
|
||
// tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0, 3);
|
||
// tabBarController.tabBar.layer.masksToBounds = NO;
|
||
// tabBarController.tabBar.clipsToBounds = NO;
|
||
|
||
// NO.1,using Image note:recommended.推荐方式
|
||
// set the bar shadow image
|
||
// without shadow : use -[[CYLTabBarController hideTabBarShadowImageView] in CYLMainRootViewController.m
|
||
if (@available(iOS 13.0, *)) {
|
||
UITabBarItemAppearance *inlineLayoutAppearance = [[UITabBarItemAppearance alloc] init];
|
||
// fix https://github.com/ChenYilong/CYLTabBarController/issues/456
|
||
inlineLayoutAppearance.normal.titlePositionAdjustment = titlePositionAdjustment;
|
||
|
||
// set the text Attributes
|
||
// 设置文字属性
|
||
[inlineLayoutAppearance.normal setTitleTextAttributes:normalAttrs];
|
||
[inlineLayoutAppearance.selected setTitleTextAttributes:selectedAttrs];
|
||
|
||
UITabBarAppearance *standardAppearance = [[UITabBarAppearance alloc] init];
|
||
standardAppearance.stackedLayoutAppearance = inlineLayoutAppearance;
|
||
standardAppearance.backgroundColor = [UIColor cyl_systemBackgroundColor];
|
||
//shadowColor和shadowImage均可以自定义颜色, shadowColor默认高度为1, shadowImage可以自定义高度.
|
||
standardAppearance.shadowColor = [UIColor cyl_systemGreenColor];
|
||
// standardAppearance.shadowImage = [[self class] imageWithColor:[UIColor cyl_systemGreenColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1)];
|
||
self.tabBar.standardAppearance = standardAppearance;
|
||
} else {
|
||
// Override point for customization after application launch.
|
||
// set the text Attributes
|
||
// 设置文字属性
|
||
UITabBarItem *tabBar = [UITabBarItem appearance];
|
||
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
|
||
[tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
|
||
|
||
// This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
|
||
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
|
||
// [[UITabBar appearance] setShadowImage:[[UIImage alloc] qmui_imageWithTintColor:UIColor.redColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1)]];
|
||
}
|
||
}
|
||
*/
|
||
|
||
@end
|