UITabBarController的一些基础设置

利用代码添加UITabBarController

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    ViewController *vc1 = [[ViewController alloc] init];
    
    UserGuideViewController *vc2 = [[UserGuideViewController alloc] init];
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
    
    for(int i=0; i<tabBarController.tabBar.items.count; i++) {
        UITabBarItem *item = [tabBarController.tabBar.items objectAtIndex:i];
        
        item.title = [NSString stringWithFormat:@"子栏目%d",i];
        
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor, nil];
        [item setTitleTextAttributes:dict forState:UIControlStateNormal];
        
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
                               UITextAttributeTextColor,nil];
        [item setTitleTextAttributes:dict2 forState:UIControlStateSelected];
    }
    
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    
    return YES;
}
复制代码

 

改变TabBar的背景色

方法一:

UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
bgView.backgroundColor = [UIColor redColor];
[self.tabBar insertSubview:bgView atIndex:0];

方法二:

复制代码
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.tabBar.backgroundImage = image;
复制代码

 

改变TabBarItem的(选择/未选择)背景图片

方法一,改变全局:

UIImage *bgImage = [UIImage imageNamed:@"Image1"];
[[UITabBar appearance] setBackgroundImage:[bgImage resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"Image2"]];

方法二,改变特定:

[self.tabBar setBackgroundImage:[UIImage imageNamed:@"GuideImage1"]];
[self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"GuideImage2"]];

 

改变TabBarItem的选择与未选择图标

复制代码
- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *carIcon = [UIImage imageNamed:@"CarIcon"];
    UIImage *grayCarIcon = [UIImage imageNamed:@"GrayCarIcon"];

    
    ViewController *vc1 = [[ViewController alloc] init];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"栏目A" image:carIcon tag:0];
    item1.selectedImage = grayCarIcon;
    vc1.tabBarItem = item1;
    
    
    UserGuideViewController *vc2 = [[UserGuideViewController alloc] init];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"栏目B" image:carIcon tag:0];
    item2.selectedImage = grayCarIcon;
    vc2.tabBarItem = item2;
    
    self.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
    
    [self setSelectedViewController:vc2];
}
复制代码

 

改变TabBarItem的文本颜色

UITabBarItem *item = self.tabBar.items[0];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor, nil];
[item setTitleTextAttributes:dict forState:UIControlStateNormal];

NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
                       UITextAttributeTextColor,nil];
[item setTitleTextAttributes:dict2 forState:UIControlStateSelected];

 

 

posted @   CoderWayne  阅读(8073)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示