2017-08-09 47 views
1

我有UIView有高度限制。我想隱藏這個觀點,所以我設置高度約束恆爲0。Swift - 具有零高度約束的UIView - 錯誤

我設置的約束在裏面的代碼擴展UIView

_height = self.heightAnchor.constraint(equalToConstant: 50) 

NSLayoutConstraint.activate([ 

     _closeBtn.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5), 
     _closeBtn.topAnchor.constraint(equalTo: self.topAnchor, constant: 10), 
     _closeBtn.heightAnchor.constraint(equalTo: _centerLabel.heightAnchor), 
     _closeBtn.widthAnchor.constraint(equalTo: _closeBtn.heightAnchor), 

     _centerLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 10), 
     _centerLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5), 
     _centerLabel.leadingAnchor.constraint(equalTo: _closeBtn.trailingAnchor), 
     _centerLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16), 

     _height 
    ]) 

現在,如果我改變_height.constant = 0,我得到這個錯誤:

[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:0x146a374c0 UIButton:0x147615b00'X'.width == UIButton:0x147615b00'X'.height (active)>", 
    "<NSLayoutConstraint:0x146a37530 UIButton:0x147615b00'X'.height == UILabel:0x147615e80.height (active)>", 
    "<NSLayoutConstraint:0x146a37450 V:|-(20)-[UILabel:0x147615e80] (active, names: '|':InternetConnectionInfoView:0x14742f600)>", 
    "<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (active)>", 
    "<NSLayoutConstraint:0x146a37680 InternetConnectionInfoView:0x14742f600.height == 0 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x146a374c0 UIButton:0x147615b00'X'.width == UIButton:0x147615b00'X'.height (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. 

[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:0x146a37530 UIButton:0x147615b00'X'.height == UILabel:0x147615e80.height (active)>", 
    "<NSLayoutConstraint:0x146a37450 V:|-(20)-[UILabel:0x147615e80] (active, names: '|':InternetConnectionInfoView:0x14742f600)>", 
    "<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (active)>", 
    "<NSLayoutConstraint:0x146a37680 InternetConnectionInfoView:0x14742f600.height == 0 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x146a373e0 UILabel:0x147615e80.bottom == InternetConnectionInfoView:0x14742f600.bottom - 5 (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. 

我懷疑,這是來自頂部和底部的錨被設置爲常量值。我試圖使用greaterThanOrEual/lessThanOrEqual,但它破壞了佈局。

如何解決這個問題(比如隱藏UIView)?我無法設置isHidden,因爲其他UIViews被錨定到這個視圖的底部,他們需要在隱藏時移動。

+0

我發現將UIView的高度設置爲0也意味着您需要將子視圖isHidden設置爲true。你嘗試過嗎?編輯:我有一個視圖,是一個滑動滑動菜單。它的動畫效果很好 - 只需爲子視圖設置「isHidden」,然後將主視圖的高度設置爲0,然後爲其設置動畫效果。 – dfd

+0

@dfd不工作,同樣的錯誤 –

+0

更改其中一個約束的優先級爲999. – Sulthan

回答

1

這是一個約束衝突,考慮減少約束優先級可以幫助你解決這個問題。

let closeBtnTopAnchor = _closeBtn.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor) 
let closeBtnTopAnchorWithLowPriority = _closeBtn.topAnchor.constraint(equalTo: self.topAnchor, constant: 10) 
closeBtnTopAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh 

let centerLabelTopAnchor = _centerLabel.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor) 
let centerLabelTopAnchorWithLowPriority = _centerLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 10) 
centerLabelTopAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh 

let centerLabelBottomAnchor = _centerLabel.bottomAnchor.constraint(lessThanOrEqualTo: self.bottomAnchor) 
let centerLabelBottomAnchorWithLowPriority = _centerLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5) 
centerLabelBottomAnchorWithLowPriority.priority = UILayoutPriorityDefaultHigh 

NSLayoutConstraint.activate([ 

    _closeBtn.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5), 
    closeBtnTopAnchor, 
    closeBtnTopAnchorWithLowPriority, 

    _closeBtn.heightAnchor.constraint(equalTo: _centerLabel.heightAnchor), 
    _closeBtn.widthAnchor.constraint(equalTo: _closeBtn.heightAnchor), 

    centerLabelTopAnchor, 
    centerLabelTopAnchorWithLowPriority, 
    centerLabelBottomAnchor, 
    centerLabelBottomAnchorWithLowPriority, 
    _centerLabel.leadingAnchor.constraint(equalTo: _closeBtn.trailingAnchor), 
    _centerLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16), 

    _height 
    ]) 
0

如果隱藏UIView對於您來說已經足夠了,那麼將它的alpha設置爲0怎麼樣?

+0

它不會移動視圖,即錨點取決於隱藏視圖的高度。 –

+0

哦,我明白了,那麼是否可以在「UIStackView」中使用這些視圖?我遇到過這種情況只有一次,並使用它解決了我的問題。 – Mango

+0

如果不是,那麼在將其設置爲0時根據高度調整這些相關約束應該可行,但我認爲這可能是一種不好的做法。 – Mango