UITabBar displays UITabBarItem image ignoring rend

2020-06-23 10:00发布

问题:

With the release of tvOS 9.1 and Xcode 7.2, my UITabBarItem images are being displayed incorrectly. In my view controllers, I set the tabBarItem.image and tabBarItem.selectedImage with images using UIImageRenderingMode.AlwaysOriginal.

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.tabBarItem.image = UIImage(named: "myTabImage")?.imageWithRenderingMode(.AlwaysOriginal)
    self.tabBarItem.selectedImage = UIImage(named: "myTabImageSelected")?.imageWithRenderingMode(.AlwaysOriginal)
}

The selected image displays correctly, but the non-selected image displays as a template, that is, its color information is ignored.

Both images displayed correctly using the tvOS 9.0 SDK, but the non-selected image is displaying incorrectly in tvOS 9.1. To make matters worse, the non-selected images are being shown as black and the tab bar background is also black.

Here is the same code running on tvOS 9.0

I suspect this is a bug with tvOS 9.1, but has anyone found a workaround or see something that I am not doing correctly?

回答1:

We have seen something similar in our tvos app, except we use text instead of images. tvOS 9.1 ignoring textColor.

UITabBarItem.appearance().setTitleTextAttributes([
    NSForegroundColorAttributeName: <barTextColor>
], forState: UIControlState.Normal)

UITabBarItem.appearance().setTitleTextAttributes([
    NSForegroundColorAttributeName: <barTextColorSelected>,
], forState: UIControlState.Selected)


回答2:

It appears to definitely be a bug in the UITabBarController implementation for tvOS 9.1. So I ended up writing my own replacement. While at it, I added support for more than 7 tab bar items, made it look nice on a black background, and included a search bar on one of the tabs (also on a black background). This solves many of the difficulties I faced trying to build my first tvOS app.

Link to Github repository



回答3:

This is was confirmed as a bug by Apple and has been fixed in tvOS 9.1.1.



回答4:

It may be help to tvOS 9.1. This code write into the viewDidLoad() of UITabBarController.

for item in self.tabBar.items!{
            item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal)
            item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.greenColor()], forState: UIControlState.Focused)
        }