SWIFT: 如果我以编程方式实现 UITAB 栏,是否可以在故事板中编辑不同的 tabItems
SWIFT: If I implement the UITAB bar programmatically, is it possible to edit the different tabItems inside the storyBoard
我已经以编程方式实现了一个 tabBar:
class ViewController: UIViewController {
let tabBarCnt = UITabBarController()
override func viewDidLoad() {
super.viewDidLoad()
tabBarCnt.tabBar.tintColor = UIColor.black
createTabBarController()
}
func createTabBarController() {
let firstVc = UIViewController()
let downloadViewController = DownloadsViewController()
downloadViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 0)
let bookmarkViewController = BookmarksViewController()
bookmarkViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
let favoritesViewControllers = FavoritesViewController()
favoritesViewControllers.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 2)
// Adding navigationControllers
let controllerArray = [downloadViewController, bookmarkViewController, favoritesViewControllers]
// For somereason this made the word Home appear in first tab
tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: [=11=])}
self.view.addSubview(tabBarCnt.view)
}
}
DownloadViewController.swift
class DownloadsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
self.title = "Downloads"
}
}
这是结果:
我的问题是:
我是否必须以编程方式实现不同 viewControllers downloadViewController, bookmarkViewController, favoritesViewControllers
的所有布局?
我不能使用故事板并关联一个 viewController 并像这样进行所有 UI 设计和实现:
这里的问题是,如果我将以编程方式实现所有布局,这将不是显而易见的或实际的事情。
并将 DownloadsViewController 分配给该 Storyboard ViewController,当我导航到 UITabBar 中的 DownloadsTab 时,它不会显示它。
是的,您当前可以通过编程方式创建视图控制器实例,而不必像这样从故事板中 load/get viewController 实例。
UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(identifier: "downloadsVC") as! DownloadsViewController
在调用此方法之前设置 viewController StoryboardID
我已经以编程方式实现了一个 tabBar:
class ViewController: UIViewController {
let tabBarCnt = UITabBarController()
override func viewDidLoad() {
super.viewDidLoad()
tabBarCnt.tabBar.tintColor = UIColor.black
createTabBarController()
}
func createTabBarController() {
let firstVc = UIViewController()
let downloadViewController = DownloadsViewController()
downloadViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 0)
let bookmarkViewController = BookmarksViewController()
bookmarkViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
let favoritesViewControllers = FavoritesViewController()
favoritesViewControllers.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 2)
// Adding navigationControllers
let controllerArray = [downloadViewController, bookmarkViewController, favoritesViewControllers]
// For somereason this made the word Home appear in first tab
tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: [=11=])}
self.view.addSubview(tabBarCnt.view)
}
}
DownloadViewController.swift
class DownloadsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
self.title = "Downloads"
}
}
这是结果:
我的问题是:
我是否必须以编程方式实现不同 viewControllers downloadViewController, bookmarkViewController, favoritesViewControllers
的所有布局?
我不能使用故事板并关联一个 viewController 并像这样进行所有 UI 设计和实现:
这里的问题是,如果我将以编程方式实现所有布局,这将不是显而易见的或实际的事情。
并将 DownloadsViewController 分配给该 Storyboard ViewController,当我导航到 UITabBar 中的 DownloadsTab 时,它不会显示它。
是的,您当前可以通过编程方式创建视图控制器实例,而不必像这样从故事板中 load/get viewController 实例。
UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(identifier: "downloadsVC") as! DownloadsViewController
在调用此方法之前设置 viewController StoryboardID