簡體   English   中英

UINavigationController 和 TabBarController 以編程方式(無故事板)

[英]UINavigationController, and TabBarController programmatically (no storyboards)

目前,我的 AppDelegate 文件包含以下代碼,用於將 CustomTabBarController 建立為 rootViewController:

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = CustomTabBarController()

我希望我的應用程序始終在底部有 CustomTabBarController,但我希望每個選項卡都有一個導航控制器。 這是我用來設置我的 tabBarController 的代碼:

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = FirstViewController()
let vc2 = SecondViewController()
let vc3 = ThirdViewController()
viewControllers = [vc1, vc2, vc3]
}

這是我用來設置 FirstViewController 的代碼:

class ProfileViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VCCellId, for: indexPath) as! firstVCCell

    return cell
}
}

當組合UITabBarControllerUINavigationController ,正確的設置方法是讓UITabBarController成為rootViewController 您的UITabBarController每個選項卡都有自己的UINavigationController 因此,如果您有 4 個選項卡,您將創建 4 個UINavigationControllers

請參閱: 將導航控制器添加到選項卡欄界面


更新

你在你的更新問題添加的代碼的建立了,創造UINavigationController為每個的vc1vc2vc3

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let vc1 = UINavigationController(rootViewController: FirstViewController())
        let vc2 = UINavigationController(rootViewController: SecondViewController())
        let vc3 = UINavigationController(rootViewController: ThirdViewController())

        viewControllers = [vc1, vc2, vc3]
    }

}

在您的每個ViewController ,將title設置為您希望在選擇該選項卡時在導航欄中顯示的標題:

class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        title = "First"
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSFontAttributeName: UIFont(name: "Chalkduster", size: 27)!,
             NSForegroundColorAttributeName: UIColor.black]
    }
}

我在接受的答案的評論中注意到您想知道在哪里更改導航欄的屬性。 如果您計划對每個選項卡應用相同的自定義,您可以在 CustomTabBarController 中使用以下代碼:

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Tab Bar Customisation
        tabBar.barTintColor = .systemPink
        tabBar.tintColor = .systemTeal
        tabBar.unselectedItemTintColor = .systemGray
        tabBar.isTranslucent = false

        viewControllers = [
            createTabBarItem(tabBarTitle: "Tab 1", tabBarImage: "TabBarImg1", viewController: ViewControllerOne()),
            createTabBarItem(tabBarTitle: "Tab 2", tabBarImage: "TabBarImg2", viewController: ViewControllerTwo()),
            createTabBarItem(tabBarTitle: "Tab 3", tabBarImage: "TabBarImg3", viewController: ViewControllerThree()),
            createTabBarItem(tabBarTitle: "Tab 4", tabBarImage: "TabBarImg4", viewController: ViewControllerFour())
        ]
    }

    func createTabBarItem(tabBarTitle: String, tabBarImage: String, viewController: UIViewController) -> UINavigationController {
        let navCont = UINavigationController(rootViewController: viewController)
        navCont.tabBarItem.title = tabBarTitle
        navCont.tabBarItem.image = UIImage(named: tabBarImage)

        // Nav Bar Customisation
        navCont.navigationBar.barTintColor = .systemRed
        navCont.navigationBar.tintColor = .systemBlue
        navCont.navigationBar.isTranslucent = false
        return navCont
    }
}

就我個人而言,我喜歡這種方法,因為它使我免於在整個 ViewController 中重復代碼,並在您的自定義標簽欄類中組織標簽和導航欄代碼。

如果您不想在整個應用程序中使用相同的選項卡或導航欄自定義,您可以簡單地在各自的 ViewControllers 類中單獨自定義每個選項卡或導航欄。 但是,如果每個標簽都有不同的標簽和/或導航屬性,那么 Vacawama 的答案很可能是最好的方法!

你可能想試試這個:

viewControllers = [vc1, vc2, vc3].map{UINavigationController(rootViewController: $0)}

暫無
暫無

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

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