UITabBarController
继承自UIViewController,具备视图控制器的生命周期管理能力,是实现多界面切换的重要控件之一。为了实现纯色背景的UITabBar,保持整个APP当中使用UITabBarController的地方样式的统一,许多同学可能会在每个界面使用的地方分别设置。
全局设置纯色UITabBar
AppDelegate中didFinishLaunchingWithOptions实现:
// MARK: - 全局配置
- (void)setNavigationBarAppearance {
NSDictionary *tabbarNAttri = @{NSForegroundColorAttributeName : AXColor_hex(@"9CA6B9"),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size: FtSize(10)]};
NSDictionary *tabbarSAttri = @{NSForegroundColorAttributeName : AXColor_hex(@"242934"),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Medium" size: FtSize(10)]};
NSDictionary *naviAttri = @{NSForegroundColorAttributeName : AXColor_hex(@"242934"), NSFontAttributeName : [UIFont fontWithName:@"PingFangSC-Medium" size: FtSize(16)]};
if (@available(iOS 15.0, *)) {
[UITableView appearance].sectionHeaderTopPadding = 0;
UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
// 选中未选中颜色设置
appearance.stackedLayoutAppearance.normal.titleTextAttributes = tabbarNAttri;
appearance.stackedLayoutAppearance.selected.titleTextAttributes = tabbarSAttri;
// 背景色
appearance.backgroundColor = [UIColor whiteColor];
appearance.shadowColor = [UIColor whiteColor];
// 线条颜色
[appearance setShadowImage:[AXImageUtility createSolidColorImage:AXColor_hex(@"EDF1F6") size:CGSizeMake(Screen_Width, 0.5)]];
[appearance setBackgroundImage:[AXImageUtility createSolidColorImage:[UIColor whiteColor] size:CGSizeMake(Screen_Width, AXTabBarHeight)]];
[UITabBar appearance].scrollEdgeAppearance = appearance;
[UITabBar appearance].standardAppearance = appearance;
[UITabBar appearance].translucent = NO;
// 不透明纯色导航
UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
// 重置导航栏背景颜色和阴影
[barApp configureWithOpaqueBackground];
// 去掉半透明效果
barApp.backgroundEffect = nil;
barApp.backgroundColor = [UIColor whiteColor];
barApp.shadowColor = [UIColor whiteColor];
barApp.titleTextAttributes = naviAttri;
[UINavigationBar appearance].scrollEdgeAppearance = barApp;
[UINavigationBar appearance].standardAppearance = barApp;
// 透明度
[UINavigationBar appearance].translucent = NO;
} else if (@available(iOS 13.0, *)) {
// 底部TabBar
UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
// 选中未选中颜色设置
appearance.stackedLayoutAppearance.normal.titleTextAttributes = tabbarNAttri;
appearance.stackedLayoutAppearance.selected.titleTextAttributes = tabbarSAttri;
// 背景色
appearance.backgroundColor = [UIColor whiteColor];
appearance.shadowColor = [UIColor whiteColor];
// 线条颜色
[appearance setShadowImage:[AXImageUtility createSolidColorImage:AXColor_hex(@"EDF1F6") size:CGSizeMake(Screen_Width, 0.5)]];
[appearance setBackgroundImage:[AXImageUtility createSolidColorImage:[UIColor whiteColor] size:CGSizeMake(Screen_Width, AXTabBarHeight)]];
[UITabBar appearance].standardAppearance = appearance;
// 透明度
[UITabBar appearance].translucent = NO;
// 不透明纯色导航
UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
// 重置导航栏背景颜色和阴影
[barApp configureWithOpaqueBackground];
// 去掉半透明效果
barApp.backgroundEffect = nil;
barApp.backgroundColor = [UIColor whiteColor];
barApp.shadowColor = [UIColor whiteColor];
barApp.titleTextAttributes = naviAttri;
[UINavigationBar appearance].scrollEdgeAppearance = barApp;
[UINavigationBar appearance].standardAppearance = barApp;
// 透明度
[UINavigationBar appearance].translucent = NO;
} else {
// 底部TabBar
[[UITabBarItem appearance] setTitleTextAttributes:tabbarNAttri forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:tabbarSAttri forState:UIControlStateSelected];
[UITabBar appearance].translucent = NO;
[UITabBar appearance].backgroundColor = [UIColor whiteColor];
// 线条
[[UITabBar appearance] setShadowImage:[AXImageUtility createSolidColorImage:AXColor_hex(@"EDF1F6") size:CGSizeMake(Screen_Width, 0.5)]];
[[UITabBar appearance] setBackgroundImage:[AXImageUtility createSolidColorImage:[UIColor whiteColor] size:CGSizeMake(Screen_Width, AXTabBarHeight)]];
// 不透明纯色导航
[UINavigationBar appearance].titleTextAttributes = naviAttri;
[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
// 线条
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// 透明度
[UINavigationBar appearance].translucent = NO;
}
}
涉及方法说明
1、16进制颜色转换
// MARK: - 16进制颜色转换
static inline UIColor *AXColor_hex(NSString *color) {
color = [color stringByReplacingOccurrencesOfString:@"#" withString:@""];
color = [color stringByReplacingOccurrencesOfString:@" " withString:@""];
long red = strtoul([[color substringWithRange:NSMakeRange(0, 2)] UTF8String] , 0, 16);
long green = strtoul([[color substringWithRange:NSMakeRange(2, 2)] UTF8String] , 0, 16);
long blue = strtoul([[color substringWithRange:NSMakeRange(4, 2)] UTF8String] , 0, 16);
return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
};
2、颜色转图片
// MARK: - 创建纯色UIImage对象(指定颜色、尺寸)
+ (UIImage *)createSolidColorImage:(UIColor *)color size:(CGSize)size {
if (!color || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UITabBarController使用
1、创建AXMainTabBarModuleVC继承UITabBarController
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface AXMainTabBarModuleVC : UITabBarController
@property (nonatomic, assign) NSUInteger selectedTabBarIndex;
@end
NS_ASSUME_NONNULL_END
#import "AXMainTabBarModuleVC.h"
#import "AXNavigationController.h"
@interface AXMainTabBarModuleVC () <UITabBarControllerDelegate>
@end
@implementation AXMainTabBarModuleVC
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
self.tabBar.translucent = NO;
self.selectedTabBarIndex = 0;
// 创建子控制器
[self setUpChildVcs];
}
// MARK: - 创建子视图
- (void)setUpChildVcs {
NSArray *imageArr = @[@"axtab_msg_nor",@"axtab_app_nor",@"axtab_anyl_nor",@"axtab_mine_nor"];
NSArray *imageSelectArr = @[@"axtab_msg_pre",@"axtab_app_pre",@"axtab_anyl_pre",@"axtab_mine_pre"];
NSArray *titleName = @[@"消息",@"应用",@"BI",@"我的"];
// 1
UIViewController *page1 = [[UIViewController alloc] init];
[self setupOneChildViewController:page1 title:titleName[0] image:imageArr[0] selectedImage:imageSelectArr[0] index:0];
// 2
UIViewController *page2 = [[UIViewController alloc] init];
[self setupOneChildViewController:page2 title:titleName[1] image:imageArr[1] selectedImage:imageSelectArr[1] index:1];
// 3
UIViewController *page3 = [[UIViewController alloc] init];
[self setupOneChildViewController:page3 title:titleName[2] image:imageArr[2] selectedImage:imageSelectArr[2] index:2];
// 4
UIViewController *page4 = [[UIViewController alloc] init];
[self setupOneChildViewController:page4 title:titleName[3] image:imageArr[3] selectedImage:imageSelectArr[3] index:3];
}
// MARK: - 设置子控制器
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage index:(int)index {
UIImage *norImg = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *preImg = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
AXNavigationController *na = [[AXNavigationController alloc] initWithRootViewController:vc];
na.tabBarItem.title = title;
na.tabBarItem.image = norImg;
na.tabBarItem.selectedImage = preImg;
[self addChildViewController:na];
}
// MARK: - 点击事件
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSUInteger index = tabBarController.selectedIndex;
self.selectedTabBarIndex = index;
}
@end
2、最终效果图