我的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"];
谢谢。
答案 0 :(得分:4)
好的,首先,我假设您在故事板中设置了图像和所选图像,并且遇到了所选图像无法显示的问题(基于代码示例)你提供)。这就是我在Swift 1.2中所拥有的内容(我假设这一切都适用于早期版本)。它基于ad121的响应,但我需要进行更改才能使其正常工作。请注意,如果您不确定这是在哪里,您会在AppDelegate中想要这个。
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
}
答案 1 :(得分:2)
我建议只看一下XCode中的文档。所有文档都是用Swift和Objective C编写的,因此很容易在两种语言之间进行翻译。另请阅读apple的快速基础知识,以便更好地理解此代码转换: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 :(得分:0)
也许您应该使用图像和所选图像。 所以有两个不同的图像使用一个正常和选择
tabBarItem.image = UIImage(named:"ImageName")
和
tabBarItem.selectedImage = UIImage(named:"ImageName")
这样当选择选项卡时,它将使用selectedImage。如果未选择,则会使用正常图像。