开屏非主界面tabbarItem隐藏,点击后显示
在空白对应位置点击隐藏的tabbaritem,点击后正常显示
问题图片
问题原因
在每个ViewController里设置了tabBarItem和title,使得加载时无法及时获取,导致开屏后不显示,直到点击后才显示
问题代码
class FirstViewController: UIViewController {
lazy var Label:UILabel = {
let label = UILabel(frame: CGRect(x: 40, y: 150, width: 240, height: 44))
label.text = "第一页"
return label
}()
func setupView(){
self.view.addSubview(Label)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.title = "识别"
self.tabBarItem.image = UIImage(systemName: "arrow.3.trianglepath")
setupView()
}
}
解决方案
在SceneDelegate或者UITabBarController里面设置tabBarItem和title
运行结果
tabbar呈半透明状,受界面背景色影响
tabbar随背景颜色变化而变化
问题图片
解决方案
在SceneDelegate里面加入代码
let defaultTabBarAppearance = UITabBarAppearance()
defaultTabBarAppearance.backgroundColor = UIColor.white // 设置为你想要的固定颜色
UITabBar.appearance().standardAppearance = defaultTabBarAppearance
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = defaultTabBarAppearance
}
eg:发现更好的解决办法(结合运用更佳),删除上面代码tabbar会变成黑色背景:
UITabBar.appearance().isTranslucent = false //避免受默认的半透明色影响,关闭
UITabBar.appearance().barTintColor = UIColor.brown//设置背景颜色
UITabBar.appearance().tintColor = UIColor.green//设置选中的背景颜色
UITabBar.appearance().clipsToBounds = true //去掉上面的分割线