UITabBarController的一些基础设置

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];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值