Reputation: 1793
My dashboard view was working fine until I tried to remove the storyboard from the project. I created a new branch using git and started to remove the storyboards and ran into this problem. Here is the basic setup of how I am getting to the dashboard screen.
// AppDelegate.swift
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainTabController()
// MainTabController
let dashboard = DashboardViewController()
let dashboardNavController = UINavigationController(rootViewController: dashboard)
dashboardNavController.tabBarItem.title = "Dashboard"
viewControllers = [dashboardNavController]
// DashboardViewController.swift
import UIKit
class DashboardViewController: UIViewController {
var dashboardTitle: UILabel {
let label = UILabel()
label.text = "Defatul Dashboard Title"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(dashboardTitle)
dashboardTitle.topAnchor.constraint(equalTo: view.topAnchor).isActive = true <-- Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
}
}
If I don't add a constraint the application runs fine, as soon as I add a constraint I get the error. Any idea as to why?
Upvotes: 1
Views: 52
Reputation: 318854
The problem is that you create a new label each time you call dashboardTitle
. You need to change dashboardTitle
to:
lazy var dashboardTitle: UILabel = {
let label = UILabel()
label.text = "Defatul Dashboard Title"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
Upvotes: 4