我需要在 appdelegate swift 中添加一个自定义标签栏控制器
Posted
技术标签:
【中文标题】我需要在 appdelegate swift 中添加一个自定义标签栏控制器【英文标题】:I need to add a custom tabbar controller in appdelegate swift 【发布时间】:2016-12-22 04:57:06 【问题描述】: 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 创建一个自定义标签栏。上面的代码是用 appdelegate 编写的目标 c 代码。
【问题讨论】:
Objectivec2Swift 我已经编辑了问题 没有。标签栏没有出现。我无法从我的页面导航到下一页。 欢迎来到 Stack Overflow!请不要编辑您的问题,以免现有答案无效。而是创建一个新问题。 【参考方案1】:试试这个
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)
【讨论】:
对于此代码 tabBarItem1?.setFinishedSelectedImage(UIImage(named: "home_selected.png")!, withFinishedUnselectedImage: UIImage(named: "home.png")!) tabBarItem2?. 它显示 'setFinishedSelectedImage(_:withFinishedUnselectedImage:)' 不可用:使用 initWithTitle:image:selectedImage: 或 image 和 selectedImage 属性以及 UIImageRenderingModeAlwaysOriginal 你可以直接给出标题、图片和从storyboard中选择的图片 使用这个tabBarItem1?.image = UIImage(named: "home.png")! tabBarItem1?.selectedImage = UIImage(named: "home_selected.png")! 完成。谢谢你。需要更多帮助。我需要将我的 rootviewcontroller 设置为“MyViewController”类。我怎样才能在上面的代码中实现这一点?请帮忙【参考方案2】:转换为 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()
【讨论】:
@Rakesh 你不使用情节提要吗? 我正在使用 xib。 你能告诉我如何在 tabbarcontroller 中设置 rootview 控制器吗?我需要将 rootview 设置为我的“MyVIewCOntroller”类。 @Rakesh 你想在 TabBarController 之前显示MyVIewCOntroller
吗?
是的。确切地。我需要它。但是我们可以在不需要标签栏功能的视图控制器中手动隐藏标签栏吗?以上是关于我需要在 appdelegate swift 中添加一个自定义标签栏控制器的主要内容,如果未能解决你的问题,请参考以下文章
我需要在 appdelegate swift 中添加一个自定义标签栏控制器
swift 在AppDelegate.swift中不需要`@UIApplicationMain`
我如何从目标 c 代码中获取 Swift AppDelegate 的参考
在 Swift 中从 AppDelegate 加载嵌入到 UINavigationController 的控制器