OK, so here's the relevant bit of my code:
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var questionView: QuestionView!
@IBOutlet weak var answerView: AnswerView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint! // Active at start, attaches tableView.topAnchor to questionView.bottomAnchor
@IBOutlet weak var tableViewTopAlternateConstraint: NSLayoutConstraint! // Inactive at start, attaches tableView.topAnchor to answerView.bottomAnchor
func submitAnswer() {
tableViewTopConstraint.isActive = false
tableViewTopAlternateConstraint = true
}
func newQuestion() {
tableViewTopConstraint.isActive = true
tableViewTopAlternateConstraint = false
}
}
I'm building a question/answer type of app and the two subviews are different sizes for design reasons. I have it set so that it toggles between the two subviews depending on the current state (toggling back for a new question when the user taps to advance), and it works fine...
...until I go to a different tab on the UITabView
that encloses everything (e.g. to change the settings then resume testing)
The moment the tab changes it's like the NSLayoutConstraint
outlets no longer exist. The orders to change them still process (I've verified this in the console), but they do nothing.
- I've tried declaring the outlets as strong (there was no difference in behavior)
- I've removed the outlets and managed them in code (it worked the first time, but afterward it stretched the shorter
QuestionView
's height to match tableView
's new top position instead of moving tableView
up to the bottom of QuestionView
as was intended)
- I've tried implementing a
UITabViewController
so that I could call tabBar(_:didSelect:)
and replace the view controller with a brand new instance of MyViewController()
whenever I switch back to that tab (It loads the first time, but when I try to switch tabs it finds nil
when accessing any of the IBOutlet
s...even though I didn't tap on the tab for the testing view and filtered by item.tag
when replacing the existing view controller)
Any suggestions on how else to attack this problem?