我对MDCTabBarViewController有问题,因为所有ViewController都显示为空(例如:添加带有故事板或通过view.addSubview()
的视图),但似乎只有ViewController的背景受到影响
(例如:view.backgroundColor = UIColor.green
)
选项卡视图控制器:
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]
}
}
}
所有FeedViewController、TableViewController和ToDoController都是新创建的文件,其中没有代码。
当
view.backgroundColor = UIColor.green
添加标签时
view.addSubview(label)
提前谢谢你!!
更新:
当通过故事板添加一个视图并试图访问它时,应用程序崩溃,说标签为零
FEEDVIEW控制器:
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")
}
}
错误:致命错误:在展开可选值时意外找到nil
最佳答案
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
view?.backgroundColor = UIColor.green
print(test?.text ?? "unable to fetch")
}
关于ios - 使用MDCTabBarViewController时空白的ViewController-Swift 4.2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52414856/