如何显示 SWIFT 的 UITabBar 项目的选定状态
How to show selected state of UITabBar Items for SWIFT
我的 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"];
谢谢。
也许你应该使用图像和选定的图像。
所以有 2 个不同的图像使用一个用于正常和选择
tabBarItem.image = UIImage(named:"ImageName")
和
tabBarItem.selectedImage = UIImage(named:"ImageName")
That way when the tab is selected it will use selectedImage.如果未选中,它将使用普通图像。
我建议只查看 XCode 中的文档。所有文档均以 Swift 和 Objective C 编写,因此两种语言之间的翻译非常容易。另请阅读苹果的 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")
好的,所以首先,我假设您正在故事板中设置图像和所选图像,并且 运行 遇到所选图像未显示的问题(基于您提供的代码示例)。这是我在 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
}
我的 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"];
谢谢。
也许你应该使用图像和选定的图像。 所以有 2 个不同的图像使用一个用于正常和选择
tabBarItem.image = UIImage(named:"ImageName")
和
tabBarItem.selectedImage = UIImage(named:"ImageName")
That way when the tab is selected it will use selectedImage.如果未选中,它将使用普通图像。
我建议只查看 XCode 中的文档。所有文档均以 Swift 和 Objective C 编写,因此两种语言之间的翻译非常容易。另请阅读苹果的 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")
好的,所以首先,我假设您正在故事板中设置图像和所选图像,并且 运行 遇到所选图像未显示的问题(基于您提供的代码示例)。这是我在 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
}