以下是一个自定义 UITabBarController 的 OC 代码示例:

TabBarController.h 文件:

#import <UIKit/UIKit.h>

@interface TabBarController : UITabBarController

@end

TabBarController.m 文件:

#import "TabBarController.h"

@interface TabBarController ()

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建并设置自定义的 UITabBar 样式
    UITabBar *tabBar = self.tabBar;
    tabBar.barTintColor = [UIColor whiteColor];
    tabBar.tintColor = [UIColor blueColor];
    tabBar.translucent = NO;
    
    // 创建并设置自定义的 UITabBarItem
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:'Item 1' image:nil tag:0];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:'Item 2' image:nil tag:1];
    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:'Item 3' image:nil tag:2];
    
    // 设置 UITabBarItem 的标题颜色
    [item1 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
    [item1 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateSelected];
    [item2 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
    [item2 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateSelected];
    [item3 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
    [item3 setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateSelected];
    
    // 设置 UITabBarItem 的图标
    item1.image = [UIImage imageNamed:'item1_normal'];
    item1.selectedImage = [UIImage imageNamed:'item1_selected'];
    item2.image = [UIImage imageNamed:'item2_normal'];
    item2.selectedImage = [UIImage imageNamed:'item2_selected'];
    item3.image = [UIImage imageNamed:'item3_normal'];
    item3.selectedImage = [UIImage imageNamed:'item3_selected'];
    
    // 将自定义的 UITabBarItem 设置给对应的 UIViewController
    UIViewController *viewController1 = self.viewControllers[0];
    viewController1.tabBarItem = item1;
    UIViewController *viewController2 = self.viewControllers[1];
    viewController2.tabBarItem = item2;
    UIViewController *viewController3 = self.viewControllers[2];
    viewController3.tabBarItem = item3;
}

@end

在使用时,将 TabBarController 作为根视图控制器使用,即可实现自定义的 UITabBarController。
iOS 开发:自定义 UITabBarController 的 OC 代码示例

原文地址: http://www.cveoy.top/t/topic/o9jQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录