繁体   English   中英

在视图 controller 内创建 stackView 崩溃,并出现“无法使用锚激活约束”

[英]creating stackView inside of view controller crashes with 'Unable to activate constraint with anchors'

我正在尝试在我的视图控制器中创建一个非常简单的 stackView。 我希望 stackView 覆盖整个屏幕。 我如何创建 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)
}

}

当我运行它时,我的应用程序崩溃并显示

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

没有冲突的约束,因为 stackView 是唯一被设置的锚。

每次在这里调用stackView时,都会从 function 内部init一个新的 Stack View 实例。 这就是您调用stackView.anchor时出现错误的原因,因为它是自动布局stackView的新实例,而不是您将子视图添加到VC中的stackView

您应该做的是创建一个变量来存储您的stackView实例,以便stackView初始化一次并在VC中全部重用。

除此之外,我们使用leadingAnchor代替leftAnchor trailingAnchor而不是rightAnchor

代码将是这样的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM