UITabBarItem setFinishedSelectedImage: deprecated in iOS7

前端 未结 3 1367
礼貌的吻别
礼貌的吻别 2020-12-14 09:17

setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated in iOS7. Apple recommends to use setters of image and selectedImage

3条回答
  •  囚心锁ツ
    2020-12-14 09:33

    The setFinishedSelectedImage is deprecated because Apple wants to direct interfaces toward using the template images and the tintColor you select (or the Blue default.) So, the easy default does the Tab Bar Items that way.

    If you need to still use the Icon image as designed, you create the image with the rendering mode for Always Original. Like:

    [[UIImage imageNamed:@"YourIcon.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    

    To set the icon you need to get the tabBarItem from the ViewController. What I missed in my app was that each of my tabs had a NavigationController wrapping the top view controller in the tab. Most answers about this subject don't mention getting the navigationContoller and that was key to getting it working in my app.

    So, in my UITableViewContoller subclass I added the following to viewDidLoad.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    ...
    
    [self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"]
     imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
    
    }
    

    The result was the Icon as designed when the tab was selected. If you leave off the imageWithRenderingMode method call, the Icon will be treated as a template colored with the tintColor. Hope this helps.

提交回复
热议问题