I created a UILabel programmatically and added it to my view controller. The view controller is enclosed in a PageViewController.
The line "Main Thread Checker: UI API called on a background thread: -[UIView init]" is showing on line 4(see below).
I haven't seen this error before. I have never had to add a label to a view on the main thread so I am not sure why this message is showing. Here is my code so far.
1. class QuizViewController: UIViewController {
2.
3. let questionLabel : UILabel = {
4. let label = UILabel()
5. label.translatesAutoresizingMaskIntoConstraints = false
6. label.textAlignment = .center
7. label.font = .systemFont(ofSize: 18, weight: .bold)
8. label.numberOfLines = 0
9. return label
10. }()
11.
12. var stackView : UIStackView = {
13. var stackView = UIStackView()
14. stackView.axis = .vertical
15. stackView.translatesAutoresizingMaskIntoConstraints = false
16. stackView.distribution = .fillEqually
17. stackView.spacing = 20
18. return stackView
19. }()
20.
21. var score = 0
22. var tag = 0
23.
24. var question : String
25. var answers : [String]
26.
27. init(question : Question) {
28. self.question = question.question
29. self.answers = question.incorrectAnswers
30. self.answers.append(question.correctAnswer)
31. super.init(nibName: nil, bundle: nil)
32. }
33.
34. required init?(coder: NSCoder) {
35. fatalError("init(coder:) has not been implemented")
36. }
37.
38. override func viewDidLoad() {
39. super.viewDidLoad()
40.
41.
42.
43. self.configureUI()
44. self.setup()
45.
46. }
47.
48. func configureUI() {
49. questionLabel.text = question
50. for answer in answers {
51. let button = UIButton()
52. button.setTitle(answer, for: .normal)
53. button.layer.cornerRadius = 20
54. button.layer.borderColor = UIColor.systemRed.cgColor
55. button.titleLabel?.numberOfLines = 0
56. button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
57. button.tag = tag
58. tag += 1
59. button.backgroundColor = .secondarySystemFill
60. stackView.addArrangedSubview(button)
61. }
62. }
63.
64. private func setup() {
65. view.addSubview(questionLabel)
66. view.addSubview(stackView)
67. view.backgroundColor = .systemBackground
68. NSLayoutConstraint.activate([
69. questionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 80),
70. questionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
71. questionLabel.heightAnchor.constraint(equalToConstant: 80),
72. questionLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
73.
74. stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
75. stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
76. stackView.heightAnchor.constraint(equalToConstant: 400),
77. stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
78. ])
79. }