gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:59:13 24 4
gpt4 key购买 nike

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

谢谢。

最佳答案

好的,首先,我假设您正在您的 Storyboard中设置图像和选定图像,并且遇到了选定图像不显示的问题(根据您提供的代码示例)。这是我在 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
}

关于ios - 如何显示 SWIFT 的 UITabBar 项目的选定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com