2017-06-30 34 views
0

試圖添加一個stackView和一些孩子到它,但得到佈局錯誤。我究竟做錯了什麼?與UIStackView一起使用佈局定位符時出錯

當trailinAchors常數低於leadingAchors常數時,問題似乎就會發生。

self.stackview.axis = .vertical 
    self.stackview.alignment = .fill 
    self.stackview.distribution = .fill 
    self.stackview.spacing = 5 
    self.addSubview(self.stackview) 
    self.stackview.translatesAutoresizingMaskIntoConstraints = false 
    self.stackview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true 
    self.stackview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true 
    self.stackview.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true 
    self.stackview.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true 

    // Holder 
    let holderView = UIView() 
    holderView.backgroundColor = UIColor.red 
    self.stackview.addArrangedSubview(holderView) 

    // Subview 
    let view3 = UIView() 
    view3.backgroundColor = UIColor.orange 
    holderView.addSubview(view3) 

    view3.translatesAutoresizingMaskIntoConstraints = false 
    view3.topAnchor.constraint(equalTo: holderView.topAnchor, constant: 0).isActive = true 
    view3.bottomAnchor.constraint(equalTo: holderView.bottomAnchor, constant: 0).isActive = true 
    view3.leadingAnchor.constraint(equalTo: holderView.leadingAnchor, constant: 30).isActive = true 
    view3.trailingAnchor.constraint(equalTo: holderView.trailingAnchor, constant: -30).isActive = true 

錯誤:

2017-06-30 17:38:37.124333+0200 XXXXX[83954:2132661] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x61000028b680 H:|-(30)-[UIView:0x7f8dab565720] (active, names: '|':UIView:0x7f8dab566620)>", 
    "<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)>", 
    "<NSLayoutConstraint:0x61000028b4a0 H:|-(0)-[UIStackView:0x7f8dab569320] (active, names: '|': XXXXX.MultipleBarGraphsView:0x7f8dab561190'What are you doing')>", 
    "<NSLayoutConstraint:0x61000028b3b0 UIStackView:0x7f8dab569320.trailing == XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.trailing (active)>", 
    "<NSLayoutConstraint:0x61000028b900 '_UITemporaryLayoutWidth' XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.width == 0 (active)>", 
    "<NSLayoutConstraint:0x61000028af00 'UISV-canvas-connection' UIStackView:0x7f8dab569320.leading == UIView:0x7f8dab566620.leading (active)>", 
    "<NSLayoutConstraint:0x61000028bbd0 'UISV-canvas-connection' H:[UIView:0x7f8dab566620]-(0)-| (active, names: '|':UIStackView:0x7f8dab569320)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30 (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

回答

0

意識到,我稱爲

layoutIfNeeded() 

在的底部功能。當我刪除它時,錯誤消失了。

0

這是什麼看法?我懷疑tableview的superview的前後錨點可能具有相同的值(相同的位置)。確保的tableview的上海華被適當地約束到其自己的上海華(可以是視圖控制器),以便該框架具有一個寬度和高度> 0