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];
tabBarItem1.title = @"Home";
tabBarItem2.title = @"Maps";
tabBarItem3.title = @"My Plan";
tabBarItem4.title = @"Settings";
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateHighlighted];
return YES;
我在目标C中找到了这个代码。我在swift中需要相同的东西。我怎么能得到这个。请帮忙,因为我是iOS新手。
我正在尝试使用swift创建自定义tabbar。上面的代码是用appdelegate编写的客观c代码。
答案 0 :(得分:0)
试试这个
var tabBarController = (self.window!.rootViewController! as! UITabBarController)
var tabBar = tabBarController.tabBar
var tabBarItem1 = tabBar.items()[0]
var tabBarItem2 = tabBar.items()[1]
var tabBarItem3 = tabBar.items()[2]
var tabBarItem4 = tabBar.items()[3]
tabBarItem1.title = "Home"
tabBarItem2.title = "Maps"
tabBarItem3.title = "My Plan"
tabBarItem4.title = "Settings"
tabBarItem1?.image = UIImage(named: "home.png")!
tabBarItem1?.selectedImage = UIImage(named: "home_selected.png")!
tabBarItem2?.image = UIImage(named: "maps.png")!
tabBarItem2?.selectedImage = UIImage(named: "maps_selected.png")!
// same for tabBarItem3,tabBarItem4 so on ....
// Change the tab bar background
var tabBarBackground = UIImage(named: "tabbar.png")!
UITabBar.appearance().backgroundImage = tabBarBackground
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!
// Change the title color of tab bar items
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
var titleHighlightedColor = UIColor(red: CGFloat(153 / 255.0), green: CGFloat(192 / 255.0), blue: CGFloat(48 / 255.0), alpha: CGFloat(1.0))
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : titleHighlightedColor], for: .highlighted)
答案 1 :(得分:0)
转换为Swift 3:
let tabBarController = (self.window!.rootViewController! as! UITabBarController)
let tabBar = tabBarController.tabBar
let tabBarItem1 = tabBar.items?[0]
let tabBarItem2 = tabBar.items?[1]
let tabBarItem3 = tabBar.items?[2]
let tabBarItem4 = tabBar.items?[3]
tabBarItem1?.title = "Home"
tabBarItem2?.title = "Maps"
tabBarItem3?.title = "My Plan"
tabBarItem4?.title = "Settings"
tabBarItem1?.image = UIImage(named: "home_selected.png")
tabBarItem2?.image = UIImage(named: "home_selected.png")
tabBarItem3?.image = UIImage(named: "home_selected.png")
tabBarItem4?.image = UIImage(named: "home_selected.png")
tabBarItem1?.selectedImage = UIImage(named: "home_selected.png")?.withRenderingMode(.alwaysOriginal)
tabBarItem2?.selectedImage = UIImage(named: "maps_selected.png")?.withRenderingMode(.alwaysOriginal)
tabBarItem3?.selectedImage = UIImage(named: "myplan_selected.png")?.withRenderingMode(.alwaysOriginal)
tabBarItem3?.selectedImage = UIImage(named: "settings_selected.png")?.withRenderingMode(.alwaysOriginal)
// Change the tab bar background
let tabBarBackground = UIImage(named: "tabbar.png")!
UITabBar.appearance().backgroundImage = tabBarBackground
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!
// Change the title color of tab bar items
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)
let titleHighlightedColor = UIColor(red: CGFloat(153 / 255.0), green: CGFloat(192 / 255.0), blue: CGFloat(48 / 255.0), alpha: CGFloat(1.0))
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: titleHighlightedColor], for: .highlighted)
修改强>
设置rootViewController:
let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
appDelegate.window?.backgroundColor = UIColor.white
appDelegate.window?.rootViewController = MyVIewCOntroller()
appDelegate.window?.makeKeyAndVisible()