Fatal error: Expected superview but found nil when attempting make constraint equalToSuperview
.
It was because I had addSubview (recordingButton)
after equalToSuperView
. If you don't fix the parent view first, you can't decide relative to the parent, so it's natural.
private func setupRecordingButton() {
recordingButton.setTitle("Record", for: .normal)
recordingButton.backgroundColor = .yellow
recordingButton.snp.makeConstraints {
$0.center.equalToSuperview()
$0.width.equalTo(100)
$0.height.equalTo(100)
}
addSubview(recordingButton)
}
The solution is to change the location of addSubview
.
private func setupRecordingButton() {
recordingButton.setTitle("Record", for: .normal)
recordingButton.backgroundColor = .yellow
addSubview(recordingButton)
recordingButton.snp.makeConstraints {
$0.center.equalToSuperview()
$0.width.equalTo(100)
$0.height.equalTo(100)
}
}
Recommended Posts