Change selected image in UITabBarItem in Swift

I am trying to change the selected image to a UITabBar. I followed the procedures listed in other Stackoverflow questions, but nothing works.

I tried to set the image in the "User Defined Runtime Attributes" section, and also tried to add the following to AppDelegate.swift:

var tabBarController = self.window!.rootViewController as UITabBarController let tabItems = tabBarController.tabBar.items as [UITabBarItem] var selectedImage0 = UIImage(named:"NewsfeedTabSelected") selectedImage0?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) tabItems[0].selectedImage = selectedImage0 

It does not give any results. I did println (tabItems [0] .title) and returned the correct title, so I know that the link to TabBarItem works.

Any thoughts?

+6
source share
3 answers

I decided to use something similar in AppDelegate.

 var tabBarController = self.window!.rootViewController as UITabBarController var tabBar = tabBarController.tabBar as UITabBar var tabBarItem1 = tabBar.items![0] as UITabBarItem var tabBarItem2 = tabBar.items![1] as UITabBarItem var tabBarItem3 = tabBar.items![2] as UITabBarItem tabBarItem1.selectedImage = UIImage(named: "FirstSelectedImage") tabBarItem2.selectedImage = UIImage(named: "SecondSelectedImage") tabBarItem3.selectedImage = UIImage(named: "ThirdSelectedImage") 
+10
source

The previous answer does not fully work. I have to install the new UIImage UIImageRenderingMode on AlwaysOriginal , this solves my situation.

below:

 import UIKit class MainTab: UITabBarController { override func viewDidLoad() { var tabBar = self.tabBar var homeSelectImage: UIImage! = UIImage(named: "firstPageSelected")?.imageWithRenderingMode(.AlwaysOriginal) var qaSelectImage: UIImage! = UIImage(named: "Q&ASelected")?.imageWithRenderingMode(.AlwaysOriginal) var mySelectImage: UIImage! = UIImage(named: "myBagSelected")?.imageWithRenderingMode(.AlwaysOriginal) (tabBar.items![0] as! UITabBarItem ).selectedImage = homeSelectImage (tabBar.items![1] as! UITabBarItem ).selectedImage = qaSelectImage (tabBar.items![2] as! UITabBarItem ).selectedImage = mySelectImage tabBar.tintColor = UIColor.greenColor() } } 

Hope this works for you

+5
source

Subclass / extension tabBarController and implement these methods (I know this is obj-c, but it should work as directly translated into swift):

 #import "const.h" #import "MainTabBarController.h" @interface MainTabBarController () @end @implementation MainTabBarController - (NSArray*)tabTitles { return @[@"Connection", @"Details", [[NSUserDefaults standardUserDefaults] objectForKey:@"LastProfileResponse"] ? @"Profile" : @"Login", @"Settings"]; } - (void)viewDidLoad { [super viewDidLoad]; NSArray *imageNames = @[@"connection_tab_", @"details_tab_", @"profile_tab_", @"settings_tab_" ]; for (int i = 0; i < self.tabBar.items.count; ++i) { ((UITabBarItem*)self.tabBar.items[i]).title = self.tabTitles[i]; ((UITabBarItem*)self.tabBar.items[i]).selectedImage = [UIImage imageNamed:[imageNames[i] stringByAppendingString:@"on"]]; ((UITabBarItem*)self.tabBar.items[i]).image = [[UIImage imageNamed:[imageNames[i] stringByAppendingString:@"off"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } self.tabBar.translucent = false; self.tabBar.barTintColor = SLATE_GREEN; self.tabBar.tintColor = YELLOW; self.delegate = self; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:WHITE, NSForegroundColorAttributeName, TAB_FONT, NSFontAttributeName, nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:YELLOW, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; } 
+1
source

Source: https://habr.com/ru/post/980835/


All Articles