簡體   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