UITabbar的背景颜色

本文介绍如何将 iOS 应用中的 TabBar 背景设置为纯白色,包括使用代码直接设置颜色和通过生成指定颜色及大小的图片来实现的方法。

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

场景:
实现背景是白色,而不是第二张图默认的灰白
(默认效果)


解决方式:
self.tabBar.backgroundColor = [UIColor whiteColor];
运行时发现没效果,查看tabbar的层级,才知道tabbar有好几层.(navigationBar也是很多层)


我的做法是这样的:
self.tabBar.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] rect:self.tabBar.bounds];

// 通过颜色生成指定大小的图片
+ (UIImage *)imageWithColor:(UIColor *)color rect:(CGRect)rect{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
return image;
}

另外还有一种做法是给tabbar添加一个同样大小白色背景的子view:
UIView *backView = [[UIView alloc]initWithFrame:self.tabBar.bounds];
backView.backgroundColor = [UIColor whiteColor];
[self.tabBar insertSubview:backView atIndex:0];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值