概述
在 iOS 13 及以上版本中,可以通过 UITabBarAppearance 配置 TabBar 的外观,实现背景透明效果。对于 iOS 15 以下版本,需使用 isTranslucent 属性。
代码示例
if #available(iOS 13.0, *) {
let appearance = UITabBarAppearance()
appearance.configureWithTransparentBackground()
// 移除阴影和边框
appearance.shadowColor = .clear
appearance.shadowImage = UIImage()
appearance.backgroundImage = UIImage()
appearance.backgroundColor = .clear
tabBar.standardAppearance = appearance
// iOS 15 需要设置 scrollEdgeAppearance
if #available(iOS 15.0, *) {
tabBar.scrollEdgeAppearance = appearance
}
} else {
tabBar.isTranslucent = true
tabBar.backgroundColor = .clear
}
说明
- 创建
UITabBarAppearance实例。 - 调用
configureWithTransparentBackground()配置透明背景。 - 清除阴影颜色、阴影图片和背景图片以确保完全透明。
- 分别设置
standardAppearance和scrollEdgeAppearance(iOS 15+)。 - 低版本系统回退到
isTranslucent方案。

