[英]Change UITabBar selectedItem in Swift
如何以編程方式更改 UITabBar 中的選定項?
從Swift 3開始,您也可以使用
tabBarController.selectedIndex = 0 // (or any other existing index)
(謝謝你,@ nidomiro。)
請嘗試以下方法
tabBar.selectedItem = tabBar.items![newIndex] as! UITabBarItem
假設你有機會獲得UITabBarController
擁有該UITabBar
,你可以做以下
self.selectedViewController = self.viewControllers![newIndex] as! UIViewController
上面的代碼行應該放在UITabBarController
子類的內部。
但是,如果您可以從“外部”訪問標簽欄控制器,請執行以下操作
tabBarController.selectedViewController = tabBarController.viewControllers![newIndex] as! UIViewController
class YOUR_TABBAR_CONTROLLER: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.selectedIndex = 0 (or any other existing index)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.