【问题标题】:UITabBarController shows blank screenUITabBarController 显示空白屏幕
【发布时间】:2020-09-21 16:01:34
【问题描述】:

我正在以编程方式启动一个没有情节提要的应用程序。当我尝试让我的 UITabBarController 成为根视图控制器时,它看起来像 背景颜色应该是红色,标签项应该是房子。可能是什么问题?

场景委托代码:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
               window = UIWindow(frame: windowScene.coordinateSpace.bounds)
               window?.windowScene = windowScene
               window?.rootViewController =  TabBarController()
               window?.makeKeyAndVisible()

    }

HomeViewController 代码:

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        // Do any additional setup after loading the view.
    }
    
}

TabBarController 代码:

import UIKit

class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupTab()
    }
    
    func setupTab(){
        let homeVC = HomeViewController()
        homeVC.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "House"), tag: 0)
        let homeNC = UINavigationController(rootViewController: homeVC)
        tabBarController?.viewControllers = [homeNC]
    }
    
}

【问题讨论】:

  • 您是否尝试过设置 tintColor (self.tabBar.tintColor = yourColor) 和/或未选中的项目 tintColor (self.tabBar.unselectedItemTintColor = yourColor)?

标签: swift


【解决方案1】:

设置选项卡视图控制器的工作需要在 SceneDelegate 中设置场景时完成。 HomeViewContoller 的 viewDidLoad 从未被调用过。

 var homeNavigationController : UINavigationController!
    var secondHomeNavigationController : UINavigationController!
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        
        let tabBarController = TabBarController()

        homeNavigationController = UINavigationController.init(rootViewController: HomeViewController())
        secondHomeNavigationController = UINavigationController.init(rootViewController: HomeViewController())
        tabBarController.viewControllers = [homeNavigationController, secondHomeNavigationController]
 
 //       let item1 = UITabBarItem(title: "Home", image: UIImage(named: "House"), tag: 0)
        let item1 = UITabBarItem(tabBarSystemItem: .featured, tag: 0)
        let item2 = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
        homeNavigationController.tabBarItem = item1
        secondHomeNavigationController.tabBarItem = item2
        
        window?.rootViewController =  tabBarController
        window?.makeKeyAndVisible()

    }

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupTab()
    }
    
    
    func setupTab(){

    }

}

class HomeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        // Do any additional setup after loading the view.
    }
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2015-10-03
    • 2020-07-17
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多