[英]Tab Bar Item Image not showing up
我在資源文件夾中有一個圖像,它沒有顯示在標簽欄上。 我將“渲染為”設置為“原始圖像”,正如其他答案所說的那樣,但這並不能解決問題。
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]
}
}
嘗試這個 。
添加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
可能有幾個原因導致這種情況發生:
1)您可能輸入了錯誤的圖像名稱
2)圖像的尺寸可能太大而無法顯示。 標簽欄項目圖像的大小應為28pt-32pt(像素)左右。
3)它也可能不是正確的格式。 標簽欄圖像應為PNG,使用一種純色呈現。 如果您嘗試在公園或某物中加載樹的JPEG,可能效果不佳。
也,
我發現如果要更改 UITabBarItem
的圖像,則需要顯式創建新的 Tab Bar項。 如果我需要“更新”並將圖像映射到UITabBarItem
我通常會從標簽欄中刪除所有項目並使用新圖像構建所有新項目。
因此,如果這是你的問題,我建議在你的視圖控制器中編寫一個函數,並在上面的viewDidLoad
,只需在你的控制器上調用該函數。
我不確定為什么,但你需要遵循相同代碼的不同順序 。 首先將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.