子类化 UITabBarController 后未显示 UITabBarItems [英] UITabBarItems not showing up after subclassing UITabBarController

查看:33
本文介绍了子类化 UITabBarController 后未显示 UITabBarItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的应用程序中的 UITabBar 添加渐变.经过几次失败的尝试后,我按照

缺少的图标(从界面生成器查看)

解决方案

不支持您正在执行的操作.但是,如果您打算这样做,则需要将渐变层放在标签栏项目的后面.你把它放在标签栏项目的前面:

 self.tabBar.layer.addSublayer(layerGradient)

你需要说更多类似的话:

 self.tabBar.layer.insertSublayer(layerGradient, at:0)

这个想法是将图层放在标签栏项目之后.但究竟要使用什么数字,我不确定;你必须进行试验,我不保证任何此类行动的稳健性.它甚至可能根本不起作用,因为标签栏项目不是视图.

为标签栏制作自定义背景的正确方法是制作一个显示渐变的 UIView,并将其设置为标签栏的 backgroundImage.你为什么不以正确的支持方式而不是你的hacky不受支持的方式来做这个?你应该使用框架,而不是对抗框架.

I've been trying to add a gradient to the UITabBar in my app. After a few failed attempts I followed the steps from this question and the. gradient now works, but the UITabBarItems are invisible (although they all work). How can I make sure that both the gradient and the UIITabBarItems are visible?

class GradientTabBarController: UITabBarController {

let layerGradient = CAGradientLayer()

override func viewDidLoad() {
    super.viewDidLoad()
    print("custom tabbar loaded")
    layerGradient.colors = [UIColor(named: "tabBarGradient1")?.cgColor, UIColor(named: "tabBarGradient2")?.cgColor]
    layerGradient.startPoint = CGPoint(x:0.5, y: 0)
    layerGradient.endPoint = CGPoint(x: 0.5, y: 1)
    layerGradient.frame = tabBar.bounds
    self.tabBar.layer.addSublayer(layerGradient)
 } 
}

what it looks like in the simulator:

The missing Icons (view from Interface Builder)

解决方案

What you're doing is not supported. However, if you're going to do it, you need to put the gradient layer behind the tab bar items. You are putting it in front of the tab bar items:

 self.tabBar.layer.addSublayer(layerGradient)

You need to say something more like:

 self.tabBar.layer.insertSublayer(layerGradient, at:0)

The idea is to put the layer behind the tab bar items. But exactly what number to use, I'm not sure; you'll have to experiment, and I don't guarantee the robustness of any such action going forward. And it might not even work at all, because the tab bar items are not views.

The correct way to make a custom background for a tab bar is to make a UIView displaying your gradient, and set it as the tab bar's backgroundImage. Why don't you do this the correct supported way instead of your hacky unsupported way? You should use the framework, not fight the framework.

这篇关于子类化 UITabBarController 后未显示 UITabBarItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆