UITabbar 相关属性设置

本文介绍如何使用Objective-C自定义iOS应用中UITabBar的背景、选中指示器及文字颜色,包括通过加载特定资源文件设定背景图片,并调整图片尺寸以适配不同状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景颜色的设置

1
2
NSString *tabbarBackgroundImage = [[NSBundle mainBundle] pathForResource:@"bg_tabbar@2x" ofType:@"png"];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:tabbarBackgroundImage]];

tabbar选择后的背景设置(这里由于图片的大小,做了一下图片的大小调整)

1
2
3
4
UIImage *image = [UIImage imageWithContentsOfFile:tabbarBackgroundImage];
CGSize size = CGSizeMake(20.0, 0.5);
UIImage *resizeImage = [self reSizeImage:image toSize:size];
[[UITabBar appearance] setSelectionIndicatorImage:resizeImage];

tabbar字体的颜色设置

1
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor, nil] forState:UIControlStateNormal];

加一个图片重新调整的函数

1
2
3
4
5
6
7
8
9
10
- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
     
    return reSizeImage;
     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值