简体   繁体   中英

How to show selected state of UITabBar Items for SWIFT

The following code in my appDelegate works for Objective-C to show selected state of custom UITabBar Items. Despite my best efforts, I cannot figure out how to translate this code to Swift. Can someone point me in the right direction?

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

[[UITabBar appearance] setTintColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //make all text and icons in tab bar the system blue font
tabBarItem1.selectedImage = [UIImage imageNamed:@"815-car-selected@2x.png"]; 
tabBarItem2.selectedImage = [UIImage imageNamed:@"742-wrench-selected@2x.png"];
tabBarItem3.selectedImage = [UIImage imageNamed:@"710-folder-selected@2x.png"];
tabBarItem4.selectedImage = [UIImage imageNamed:@"724-info-selected@2x.png"];

Thank you.

OK, so first off, I assume you're setting your Image and Selected Image in your Story Board, and are running into the issue where the selected image doesn't show up (based on the code sample you provided). Here's what I have in Swift 1.2 (I assume this all works in earlier versions as well). Its based on the response from ad121, but with changes I needed to make it work correctly. And note, you'll want this in your AppDelegate in case you're unsure where this goes.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    // Type casting in swift is "as Type", you'll need to unwrap optionals however.
    let tabBarController = self.window!.rootViewController as! UITabBarController
    let tabBar = tabBarController.tabBar as UITabBar

    // I prefer to use 0 based labels since the array is 0 based
    let tabBarItem0 = tabBar.items![0] as! UITabBarItem
    let tabBarItem1 = tabBar.items![1] as! UITabBarItem
    let tabBarItem2 = tabBar.items![2] as! UITabBarItem
    let tabBarItem3 = tabBar.items![3] as! UITabBarItem

    // The UIColor method you are using is an initializer in swift
    tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)

    // Using Assets with the various sizes loaded (1x, 2x, 3x) is better.
    tabBarItem0.selectedImage = UIImage(named: "815-car-selected")
    tabBarItem1.selectedImage = UIImage(named: "742-wrench-selected")
    tabBarItem2.selectedImage = UIImage(named: "710-folder-selected")
    tabBarItem3.selectedImage = UIImage(named: "724-info-selected")

    return true
}

I recommend just looking at the documentation in XCode. All documentation is written in Swift and Objective C so it is very easy to translate between the two languages. Also read apple's swift basics to understand this code translation better: https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_467

Translation:

// Type casting in swift is "as Type"
tabBarController = self.window.rootViewController as UITabBarController
tabBar = tabBarController.tabBar
// Retrieving array values at indices can be shortened as array[index]
tabBarItem1 = tabBar.items[0] as UITabBarItem
tabBarItem2 = tabBar.items[1] as UITabBarItem
tabBarItem3 = tabBar.items[2] as UITabBarItem
tabBarItem4 = tabBar.items[3] as UITabBarItem

// The UIColor method you are using is an initializer in swift
tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)

// UIImage also has an initializer for your situation in swift
tabBarItem1.selectedImage = UIImage(named: "815-car-selected@2x.png")
tabBarItem2.selectedImage = UIImage(named: "742-wrench-selected@2x.png")
tabBarItem3.selectedImage = UIImage(named: "710-folder-selected@2x.png")
tabBarItem4.selectedImage = UIImage(named: "724-info-selected@2x.png")

Maybe you should use image and selected image. So have 2 different images use one for normal and selected

tabBarItem.image = UIImage(named:"ImageName")

and

tabBarItem.selectedImage = UIImage(named:"ImageName")

That way when the tab is selected it will use selectedImage. If it's not selected it will use the normal image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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