Swift 创建选项卡栏控制器和导航控制器
我有一个应用程序,但使用的是XIB文件,所以如果我在应用程序委托中添加此代码,以创建选项卡栏控制器Swift 创建选项卡栏控制器和导航控制器,swift,xib,Swift,Xib,我有一个应用程序,但使用的是XIB文件,所以如果我在应用程序委托中添加此代码,以创建选项卡栏控制器 let tabBarController = UITabBarController() let tabViewController1 = DummyViewController( nibName: "DummyViewController", bundle: nil) let tabViewController2 = SearchViewContro
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
let viewController = SearchViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
//Create navigation controller
let navigationController = UINavigationController(rootViewController: tabBarController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController//Set navigation controller as window's root view
self.window?.makeKeyAndVisible()
这段代码用来创建导航控制器
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
let viewController = SearchViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
//Create navigation controller
let navigationController = UINavigationController(rootViewController: tabBarController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController//Set navigation controller as window's root view
self.window?.makeKeyAndVisible()
不能,因为我同时添加了self.window?.rootViewController=navigationController和window?.rootViewController=tabBarController。我想要的是这样的东西:
但在代码中,我需要导航控制器来推送视图控制器。我是否可以建议一种更简单的方法,从情节提要中设置选项卡栏控制器往往会变得非常复杂,而且随着应用程序规模的增长,很难维护。相反,从appdelegate创建它并修改didFinishLaunchingWithOptions方法会容易得多。在这个解决方案中,我显示了两个选项卡。我演示了如何从故事板设置一个选项卡,从视图控制器设置另一个选项卡,您可以通过编程方式进行设置。我还将演示如何自定义选项卡栏,以及如何自定义在实现选项卡栏控制器时显示的导航栏
//this will hold the root
var rootController: UIViewController!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//modify didFinishLaunchingWithOptions in your app delegate as follows
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabController = UITabBarController()
//setup a view controller from another storyboard
let workoutsStoryboard = UIStoryboard(name: "Workouts", bundle: nil)
//this tab will start from a storyboard of its own
let homeVC = workoutsStoryboard.instantiateViewController(withIdentifier: "home") as! HomeViewController
//this will setup another tab bar but from a view controller only if you want to setup things programmatically
let profileVC = ProfileViewController()
//setup the tab bar elements with the icons, name and initial view controllers
let vcData: [(UIViewController, UIImage, String)] = [
(homeVC, UIImage(named: "home_tabbar_icon")!, "Home"),
(profileVC, UIImage(named: "feed_tabbar_icon")!, "Profile")
]
let vcs = vcData.map { (vc, image, title) -> UINavigationController in
let nav = UINavigationController(rootViewController: vc)
nav.tabBarItem.image = image
nav.title = title
return nav
}
//customize your tab bar
tabController.viewControllers = vcs
tabController.tabBar.barTintColor = UIColor(hexString: "#FAFAFA")
tabController.tabBar.tintColor = UIColor(hexString: "#4A4A4A")
tabController.tabBar.isTranslucent = false
if let items = tabController.tabBar.items {
for item in items {
if let image = item.image {
item.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
}
//make your tab bar the root
window?.rootViewController = tabController
//tab bar comes with a nav bar. here is how to customize it
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().backgroundColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(hexString: "#4A90E2")
rootController = (window?.rootViewController)!
return true
}
如果您想知道如何在上面的示例中从家中设置故事板,那么您需要做的就是转到New->File并选择storyboard。调用情节提要主页,当它进入项目时,选择它,并在文件检查器中将其名称更改为Home.storyboard
现在,您可以转到该情节提要,添加您想要作为初始视图控制器的导航控制器以及所有其他跟随它的视图控制器
我鼓励为每个选项卡设置一个故事板。它保持事物的清洁和分离。或者,您可以通过编程实现,而无需添加情节提要,只需从视图控制器文件进行设置。都是一样的。在didFinishLaunchingWithOptions下编写以下代码:-
//创建选项卡控制器
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
let viewController = SearchViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
let tabBarController = UITabBarController()
let tabViewController1 = DummyViewController(
nibName: "DummyViewController",
bundle: nil)
let tabViewController2 = SearchViewController(
nibName:"SearchViewController",
bundle: nil)
tabViewController1.tabBarItem = UITabBarItem(
title: "Location",
image: UIImage(named: "ic_location_blue"),
tag: 1)
tabViewController2.tabBarItem = UITabBarItem(
title: "Search",
image:UIImage(named: "ic_search_blue") ,
tag:2)
let controllers = [tabViewController1,tabViewController2]
tabBarController.viewControllers = controllers
//Create navigation controller
let navigationController = UINavigationController(rootViewController: tabBarController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController//Set navigation controller as window's root view
self.window?.makeKeyAndVisible()
仅供参考,如果上述代码不符合您的要求,请将DummyViewController和SearchViewController放在单独的导航控制器中,如tabNAVViewController1、tabNavViewController2。然后'let controllers=[tabNAVViewController1,tabNavViewController2]`