簡體   English   中英

如何以編程方式創建UITabBar並在選項卡之間切換?

[英]How to create UITabBar programatically and switch between tabs?

我下面有用於創建UITabBar代碼,但不知道如何為選項卡添加操作以在它們之間進行切換。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let item1 = UITabBarItem()
        let item2 = UITabBarItem()
        let bar = UITabBarController()
        let view1 = UIViewController()
        let view2 = UIViewController()

        bar.viewControllers = [view1 , view2]
        let tab = bar.tabBar as UITabBar
        item1.title = "Hi"
        item2.title = "bye"
        view.addSubview(tab)
        view1.tabBarItem = item1
        view2.tabBarItem = item2



    }
}

由於您的意圖似乎是在切換視圖控制器,因此您應該使用UITabBarController。 然后,通過分配selectedViewController或selectedIndex來切換ViewController。 了解更多: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/selectedViewController

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM