要将TabBar添加到基于NavigationController的iPhone应用程序,您可以遵循以下步骤:
以下是一个简单的代码示例:
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = UINavigationController(rootViewController: FirstViewController())
firstViewController.tabBarItem = UITabBarItem(title: "First", image: UIImage(systemName: "square.grid.2x2"), tag: 0)
let secondViewController = UINavigationController(rootViewController: SecondViewController())
secondViewController.tabBarItem = UITabBarItem(title: "Second", image: UIImage(systemName: "list.bullet"), tag: 1)
let tabBarList = [firstViewController, secondViewController]
viewControllers = tabBarList
}
}
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
在AppDelegate.swift文件中:
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabBarController = TabBarController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
}
这将创建一个带有两个选项卡的TabBar,分别是“First”和“Second”,并将它们添加到基于NavigationController的iPhone应用程序中。您可以根据需要添加更多选项卡和视图控制器。
领取专属 10元无门槛券
手把手带您无忧上云