简体   繁体   English

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

[英]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.

相关问题 无法使用锚点激活约束 - Unable to activate constraint with anchors 添加文本字段时无法使用锚激活约束 - Unable to activate constraint with anchors while adding a textfield 无法使用 UIimageView 的锚点错误激活约束 - Unable to activate constraint with anchors error with UIimageView Xcode 11.5 更新后错误:无法使用锚激活约束 - Xcode 11.5 Post-Update Error: Unable to activate constraint with anchors 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚点激活约束 - Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors 为stackView中的项目设置最大高度的约束 - Constraint to set max height for items inside stackView 如何向 StackView 的子视图内的视图添加约束 - How to add constraints to a view inside a subview of a StackView 在Swift中以编程方式设置UI约束(UIView中的Image和stackView) - Setting up UI constraint (Image and stackView inside a UIView) programmatically in Swift 更新stackview约束会导致崩溃:“未为约束准备视图层次结构” - Updating stackview constraints results in crash: “The view hierarchy is not prepared for the constraint” 标签栏视图控制器内的操作后,应用崩溃 - App crashes after action inside tab bar view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM