简体   繁体   English

标签栏项目图像未显示

[英]Tab Bar Item Image not showing up

I have an image in the assets folder and it isn't showing up on the tab bar. 我在资源文件夹中有一个图像,它没有显示在标签栏上。 I've set the "Render as" to "Original Image" as other answers have said to do but that doesn't fix the issue. 我将“渲染为”设置为“原始图像”,正如其他答案所说的那样,但这并不能解决问题。

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
    super.viewDidLoad()


    let homeController = HomeController(collectionViewLayout: UICollectionViewFlowLayout())
    let navigationController = UINavigationController(rootViewController: homeController)
    navigationController.tabBarItem.image = UIImage(named:"news_feed_icon")

    viewControllers = [homeController]

    }
}

Try this . 尝试这个 。

Add in viewDidLoad 添加viewDidLoad

let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "news_feed_icon")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), selectedImage: UIImage(named: "news_feed_icon"))
    self.tabBarItem = customTabBarItem

There could be a couple of reasons why this is happening: 可能有几个原因导致这种情况发生:

1) You may have entered the wrong name for the image 1)您可能输入了错误的图像名称

2) The image may be of a size that is too large to show up. 2)图像的尺寸可能太大而无法显示。 Tab Bar item images should be around 28pt-32pt (pixels) in size. 标签栏项目图像的大小应为28pt-32pt(像素)左右。

3) It also may not be the correct format. 3)它也可能不是正确的格式。 Tab Bar images should be PNG that are rendered with one solid color. 标签栏图像应为PNG,使用一种纯色呈现。 If you are trying to load a JPEG of a tree in a park or something, might not work out so well. 如果您尝试在公园或某物中加载树的JPEG,可能效果不佳。

Also, 也,

I have found you need to explicitly create a new Tab Bar item if you wish to change the image of a UITabBarItem . 我发现如果要更改 UITabBarItem的图像,则需要显式创建新的 Tab Bar项。 I typically remove all items from my tab bar and build all new items with the new image if I ever need to "update" and image to a UITabBarItem . 如果我需要“更新”并将图像映射到UITabBarItem我通常会从标签栏中删除所有项目并使用新图像构建所有新项目。

So if that is your issue I would suggest writing a function in your view controller that will do just that and in your viewDidLoad above, just call that function on your controller. 因此,如果这是你的问题,我建议在你的视图控制器中编写一个函数,并在上面的viewDidLoad ,只需在你的控制器上调用该函数。

I'm not sure why, but you need to follow a different sequence of the same code. 我不确定为什么,但你需要遵循相同代码的不同顺序 That is first add your viewController(s) to the tabBar then add its image. 首先将viewController添加到tabBar然后添加其图像。

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
    super.viewDidLoad()


    let homeController = HomeController(collectionViewLayout: UICollectionViewFlowLayout())
    let navigationController = UINavigationController(rootViewController: homeController)

    //First do this 
    viewControllers = [homeController]

    //Then add image
    navigationController.tabBarItem.image = UIImage(named:"news_feed_icon")  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM