Adrien Zier
Adrien Zier

Reputation: 816

Blank ViewControllers when using MDCTabBarViewController - Swift 4.2

I'm having issues with the MDCTabBarViewController since all ViewControllers appear blank (eg: adding views with storyboard or through view.addSubview()) but only thing seem that seen to be affected is the ViewController's background (eg: view.backgroundColor = UIColor.green)

TabViewController:

import UIKit
import MaterialComponents

class TabViewController: MDCTabBarViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //view.backgroundColor = MDCPalette.grey.tint100
    loadTabBar()
}

func loadTabBar() {

    let firstVC = FeedViewController()
    firstVC.tabBarItem = UITabBarItem(title: "Feed", image: #imageLiteral(resourceName: "feed"), tag: 0)

    let secondVC = TableViewController()
    secondVC.tabBarItem =  UITabBarItem(title: "Timetable", image: #imageLiteral(resourceName: "table"), tag: 1)

    let thirdVC = ToDoViewController()
    thirdVC.tabBarItem = UITabBarItem(title: "To-Do", image: #imageLiteral(resourceName: "todo"), tag: 2)

    let viewControllersArray = [firstVC, secondVC, thirdVC]
    viewControllers = viewControllersArray

    let childVC = viewControllers.first
    selectedViewController = childVC

    tabBar?.delegate = self

    tabBar?.items = [firstVC.tabBarItem,
                     secondVC.tabBarItem ,
                     thirdVC.tabBarItem]

    tabBar?.selectedItem = tabBar?.items.first

    tabBar?.backgroundColor = MDCPalette.lightBlue.tint500
    tabBar?.selectedItemTintColor = .white
    tabBar?.unselectedItemTintColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3)
    tabBar?.inkColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1)
    tabBar?.alignment = .justified


}

override func tabBar(_ tabBar: MDCTabBar, didSelect item: UITabBarItem) {

    switch item.tag {
    case 0:
        print("feed")
        tabBar.backgroundColor = MDCPalette.lightBlue.tint500
        selectedViewController = viewControllers[0]
    case 1:
        print("table")
        tabBar.backgroundColor = MDCPalette.purple.tint500
        selectedViewController = viewControllers[1]
    case 2:
        print("todo")
        tabBar.backgroundColor = MDCPalette.teal.tint500
        selectedViewController = viewControllers[2]
    default:
        print("feed")
        tabBar.backgroundColor = MDCPalette.lightBlue.tint500
        selectedViewController = viewControllers[0]
    }

}


}

All FeedViewController, TableViewController and ToDoController are newly created files with no code in it.

when view.backgroundColor = UIColor.green enter image description here

when adding a label view.addSubview(label) enter image description here

Thanks a bunch in advance!!

UPDATE:

when adding a view though the storyboard and trying to access it the app crashes saying the label is nil

FeedViewController:

import UIKit

class FeedViewController: UIViewController {

@IBOutlet weak var test: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.green
    print(test.text ?? "unable to fetch")
}

} 

Error: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Upvotes: 0

Views: 541

Answers (1)

Cody Winton
Cody Winton

Reputation: 2989

Try this:

override func viewDidLoad() {
    super.viewDidLoad()

    view?.backgroundColor = UIColor.green
    print(test?.text ?? "unable to fetch")
}

Upvotes: 1

Related Questions