簡體   English   中英

UITableViewCell-無法同時滿足約束

[英]UITableViewCell - unable to simultaneously satisfy constraints

我使用SnapKit來進行約束,並以編程方式創建視圖。 但是,有一件事我無法掌握。

我想將一個子UIView添加到表視圖單元格的內容視圖中並設置其高度。 這已經導致無法滿足的約束錯誤。

我也嘗試在表視圖中設置(估計)rowHeight,但這沒有幫助。

2018-07-14 19:21:38.785556+0200[20027:748731] [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. 
(
    "<SnapKit.LayoutConstraint:0x6000000b6500@WeatherCell.swift#24 UIView:0x7fa342409240.top == UITableViewCellContentView:0x7fa342413970.top>",
    "<SnapKit.LayoutConstraint:0x6000000b7880@WeatherCell.swift#24 UIView:0x7fa342409240.bottom == UITableViewCellContentView:0x7fa342413970.bottom>",
    "<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>",
    "<NSLayoutConstraint:0x604000282210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa342413970.height == 146   (active)>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>

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.

單元格代碼:

class WeatherCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let expandView = UIView()

        self.contentView.addSubview(expandView)

        expandView.snp.makeConstraints { view in
            view.edges.equalToSuperview()

            view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

您需要參考底部約束並將其優先級更改為999 /高,但不是必需的,因此刪除邊緣約束並將它們分開

let expandView = UIView()
self.contentView.addSubview(expandView)
expandView.snp.makeConstraints { (view) in
    view.bottom.equalTo(self.contentView).priority(999) // can also be .priority(.high)
    view.top.left.right.equalTo(self.contentView)
    view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM