ios - iOS奇怪的“无法使用锚激活约束”错误
问题描述
我目前正在开发的 ios 应用程序从登录屏幕开始,然后通过登录按钮显示下一个视图控制器。转到下一个 VC 后,我收到错误消息:
Terminating app due to uncaught exception 'NSGenericException',
reason: 'Unable to activate constraint with anchors
<NSLayoutYAxisAnchor:0x600000d0af40 "UIStackView:0x7fe87df14160.top"> and
<NSLayoutYAxisAnchor:0x600000d0ad40 "UILayoutGuide:0x600002102300'UIViewSafeAreaLayoutGuide'.bottom">
because they have no common ancestor.
Does the constraint or its anchors reference items
in different view hierarchies? That's illegal.'
以下是显示的控制器的相关 VC 代码:
let promptLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Enter Your Location`s \nAddress"
label.font = .boldSystemFont(ofSize: 23)
label.numberOfLines = 0
label.preferredMaxLayoutWidth = label.frame.width
label.textAlignment = .center
return label
}()
let streetAddressField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "street address"
textField.borderStyle = .roundedRect
textField.textAlignment = .left
return textField
}()
let cityField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "city"
textField.borderStyle = .roundedRect
textField.textAlignment = .left
return textField
}()
let stateField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "state"
textField.borderStyle = .roundedRect
textField.textAlignment = .left
return textField
}()
let saveButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add Location", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
var currentUser: User?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let views: [UIView] = [streetAddressField, cityField, stateField]
let stackView = UIStackView(arrangedSubviews: views)
stackView.axis = .vertical
stackView.spacing = 17
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(promptLabel)
view.addSubview(stackView)
view.addSubview(saveButton)
promptLabel.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
promptLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30).isActive = true
promptLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 70).isActive = true
stackView.topAnchor.constraint(equalTo: promptLabel.bottomAnchor, constant: 40).isActive = true
stackView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
stackView.widthAnchor.constraint(equalToConstant: 300).isActive = true
saveButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
saveButton.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 30).isActive = true
saveButton.addTarget(self, action: #selector(addLocationTouchUpInside), for: .touchUpInside)
}
奇怪的是,这个错误有一段时间没有出现。在不同的 iphone 模拟器上进行测试时,错误需要运行很多次才能显示出来。错误也显示@main
在AppDelegate.swift
.
我知道这个问题有多个重复项。然而,他们都没有将他们的子视图添加到我在这里所做的超级视图中。
更新:我已经更新到 xcode 13 并且不再看到这个错误。不幸的是,它在模拟器和我的个人 iphone 上再次抬起了丑陋的头。
UPDATE2:设置了许多断点之后,这个错误似乎甚至在viewWillAppear()
被调用之前就出现了。self.navigationController.pushViewController()
当我们看到错误时,在调用后立即返回我以前的 VC 。(self
这里当然指的是之前的VC)
解决方案
推荐阅读
- flutter - 如何将 Image.file 添加为圆形头像的背景图像
- java - java的复杂性选择
- javascript - 如何在没有 JSDoc 的情况下为给定的 .js 脚本定义和使用类型?
- javascript - 我的反应函数钩子没有在函数内返回数据状态?
- google-apps-script - Google Script:根据多个条件将行移动到不同的工作表
- react-native - 在 react-native 的屏幕之间传递数据
- c++ - 使用大括号和圆括号
- python - 如何使用 Python/selenium 循环获取鼠标悬停元素的内容?
- oracle - Oracle SQL Loader 和日期字段的问题
- sql - Big Query - 背靠背计算开始和结束日期