如何显示 SWIFT 的 UITabBar 项目的选定状态



appDelegate中的以下代码适用于Objective-C,以显示自定义UITabBar项的选定状态。尽管我尽了最大的努力,但我还是不知道如何将这段代码翻译成 Swift。 有人可以指出我正确的方向吗?

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"];

谢谢。

好的,首先,我假设您正在故事板中设置图像和所选图像,并且遇到了所选图像未显示的问题(基于您提供的代码示例)。这是我在 Swift 1.2 中拥有的内容(我认为这一切都适用于早期版本)。它基于 ad121 的响应,但通过更改,我需要使其正常工作。请注意,您需要在应用程序代表中使用它,以防您不确定它的去向。

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
}

我建议只查看XCode中的文档。所有文档都是用 Swift 和 Objective C 编写的,因此在两种语言之间进行翻译非常容易。另请阅读 apple 的 swift 基础知识,以更好地理解此代码翻译:https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_467

译本:

// 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")

也许你应该使用图像和选定的图像。所以有 2 个不同的图像使用一个用于正常和选定的

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

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

这样,当选择选项卡时,它将使用selectedImage。如果未选中,它将使用普通图像。

最新更新