您好,登錄后才能下訂單哦!
在UIKit中使用Tab Bar Controller非常簡單,只需要創(chuàng)建一個Tab Bar Controller對象并將需要顯示的視圖控制器添加到Tab Bar Controller中即可。
下面是一個簡單的示例代碼:
import UIKit
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstVC = FirstViewController()
let secondVC = SecondViewController()
firstVC.tabBarItem = UITabBarItem(title: "First", image: UIImage(named: "first"), selectedImage: UIImage(named: "first_selected"))
secondVC.tabBarItem = UITabBarItem(title: "Second", image: UIImage(named: "second"), selectedImage: UIImage(named: "second_selected"))
viewControllers = [firstVC, secondVC]
}
}
在這個示例中,我們創(chuàng)建了一個MainTabBarController
類,繼承自UITabBarController
。在viewDidLoad()
方法中,我們創(chuàng)建了兩個視圖控制器FirstViewController
和SecondViewController
,并為它們分別設(shè)置了對應(yīng)的Tab Bar Item。最后,將這兩個視圖控制器添加到viewControllers
數(shù)組中,Tab Bar Controller會自動顯示這兩個視圖控制器在Tab Bar上。
需要注意的是,要在應(yīng)用程序中使用Tab Bar Controller,通常需要在AppDelegate
中設(shè)置Tab Bar Controller為根視圖控制器,例如:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainTabBarController = MainTabBarController()
window?.rootViewController = mainTabBarController
window?.makeKeyAndVisible()
return true
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。