cdts/xdts-ios 3/TreeHole/CYHResetCode/THBaseViewController/THTabBar/THTabBarViewController.m

174 lines
7.8 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// 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;
}
/**
* TabBartabBarItem 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.2using 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.1using 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];
//shadowColorshadowImage, shadowColor1, 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