我正在将巨大的故事板重构为多个故事板。在此过程中,我努力使标签栏图像显示出来。
class CustomTabBarController: UITabBarController{
@IBOutlet weak var myTabBar: UITabBar?
override func viewDidLoad() {
super.viewDidLoad()
setTabBarItems()
}
func setTabBarItems(){
guard let tabBar1 = self.tabBar.items?[0] else { return }
let myTabBarItem1 = tabBar1 as UITabBarItem
myTabBarItem1.image = UIImage(named: "1-tab-bar-unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.selectedImage = UIImage(named: "1-tab-bar-selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.title = ""
guard let tabBar2 = self.tabBar.items?[1] else { return }
let myTabBarItem2 = tabBar2 as UITabBarItem
myTabBarItem2.image = UIImage(named: "2-tab-bar-unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem2.selectedImage = UIImage(named: "2-tab-bar-selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem2.title = ""
guard let tabBar3 = self.tabBar.items?[2] else { return }
let myTabBarItem3 = tabBar3 as UITabBarItem
myTabBarItem3.image = UIImage(named: "3")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem3.selectedImage = UIImage(named: "3-tab-bar-selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem3.title = ""
guard let tabBar4 = self.tabBar.items?[3] else { return }
let myTabBarItem4 = tabBar4 as UITabBarItem
myTabBarItem4.image = UIImage(named: "4-tab-bar-unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem4.selectedImage = UIImage(named: "4-tab-bar-selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem4.title = ""
guard let tabBar5 = self.tabBar.items?[4] else { return }
let myTabBarItem5 = tabBar5 as UITabBarItem
myTabBarItem5.image = UIImage(named: "5-tab-bar-unselected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem5.selectedImage = UIImage(named: "5-tab-bar-selected")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem5.title = ""
}
}
这是我以前的工作。目前,它仅显示“项目”中的任何项目。我已将选项卡栏项添加到导航视图控制器。
我尝试过的・来自UITabBarController的实例化VC・在设置导航视图控制器中设置导航项,此方法有效,但是在应用启动时选项卡栏为空。
感谢您的任何帮助
class CustomTabBarController: UITabBarController{
override func viewDidLoad() {
self.viewControllers![3].title = "Your Title" //This for title
self.tabBar.items?[0].image = UIImage(named: "YourImageName")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal) // This for image
}
当您的图像不来时意味着您的图像尺寸不正确,因为我也面临同样的问题。图像的尺寸可能太大而无法显示。标签栏项图像的尺寸应在28pt-32pt(像素)左右。