[英]creating stackView inside of view controller crashes with 'Unable to activate constraint with anchors'
I am trying to create a very simple stackView in my viewcontroller.我正在尝试在我的视图控制器中创建一个非常简单的 stackView。 I want the stackView to cover the entire screen.
我希望 stackView 覆盖整个屏幕。 How I am creating the stackView is
我如何创建 stackView 是
import UIKit
import Firebase
class Vc: UIViewController {
var scrollView: UIScrollView {
let scroll = UIScrollView()
scroll.isScrollEnabled = true
return scroll
}
var stackView: UIStackView {
let stack = UIStackView()
stack.axis = .vertical
stack.distribution = .fillEqually
return stack
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.tintColor = .white
self.navigationItem.title = "Profile"
self.view.backgroundColor = UIColor.white
addStackViewAnchors()
}
func addStackViewAnchors() {
view.addSubview(stackView)
stackView.anchors(top: view.safeAreaLayoutGuide.topAnchor, topPad: 0, bottom: view.safeAreaLayoutGuide.bottomAnchor, bottomPad: 0, left: view.safeAreaLayoutGuide.leftAnchor, leftPad: 0, right: view.safeAreaLayoutGuide.rightAnchor, rightPad: 0, height: .zero, width: .zero)
}
}
When I run this, my app crashes and it says当我运行它时,我的应用程序崩溃并显示
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x283c5e840 "UIStackView:0x135666a90.top"> and <NSLayoutYAxisAnchor:0x283c5e140 "UILayoutGuide:0x28103b8e0'UIViewSafeAreaLayoutGuide'.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
terminating with uncaught exception of type NSException
There are no conflicting constraints because the stackView is the only anchor being set.没有冲突的约束,因为 stackView 是唯一被设置的锚。
Every time you called stackView
here you init
a new instance of Stack View from inside your function.每次在这里调用
stackView
时,都会从 function 内部init
一个新的 Stack View 实例。 That's the reason when you called stackView.anchor
the error appears because it is autolayout the new instance of stackView
not the stackView
you add subview into your VC
.这就是您调用
stackView.anchor
时出现错误的原因,因为它是自动布局stackView
的新实例,而不是您将子视图添加到VC
中的stackView
。
The thing you should do is create a variable to store your stackView
instance so the stackView
init for once and reuse in all in the VC
.您应该做的是创建一个变量来存储您的
stackView
实例,以便stackView
初始化一次并在VC
中全部重用。
Beside of that, we use leadingAnchor
instead of leftAnchor
;除此之外,我们使用
leadingAnchor
代替leftAnchor
; trailingAnchor
instead of rightAnchor
. trailingAnchor
而不是rightAnchor
。
Code will be like this代码将是这样的
class ViewController: UIViewController {
private var stackViewInScreen : UIStackView?
var stackView: UIStackView {
let stack = UIStackView()
stack.axis = .vertical
stack.distribution = .fillEqually
return stack
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
addStackViewAnchors()
}
func addStackViewAnchors() {
self.stackViewInScreen = stackView
self.view.addSubview(self.stackViewInScreen!)
self.stackViewInScreen!.translatesAutoresizingMaskIntoConstraints = false
self.stackViewInScreen?.backgroundColor = UIColor.orange
NSLayoutConstraint.activate([
self.stackViewInScreen!.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0),
self.stackViewInScreen!.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
self.stackViewInScreen!.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0),
self.stackViewInScreen!.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 0)
])
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.