2017-12-02 243 views
1

我有一個非常簡單的設置,我無法做到我想要的。我正在嘗試添加inputAccessoryViewUIView的高度約束打破子視圖的高度約束

對於inputAccessoryView我有一個UIViewViewA)配有一個子視圖(ViewB)。 ViewB固定爲ViewA的所有邊緣。 查看B有固定的高度,需要高度爲ViewAViewB

ViewA的不是由我設定,但是0打破ViewB(設定爲42)的高度。

ViewA被添加作爲在UIViewController的視圖一個子視圖並固定在底部和兩個前緣和後緣(ViewAViewB的所以寬度應是UIViewController的視圖,其中它是)。

這裏是我的代碼:

override init(frame: CGRect) { 
    super.init(frame: frame) 

    let viewB = UIView() 
    addSubview(viewB) 

    viewB.translatesAutoresizingMaskIntoConstraints = false 
    viewB.topAnchor.constraint(equalTo: topAnchor).isActive = true 
    viewB.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true 
    viewB.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true 
    viewB.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true 
    viewB.heightAnchor.constraint(equalToConstant: 42).isActive = true 
} 

這將導致以下錯誤:

2017-12-02 15:44:59.150228-0800 Ping[37511:5808904] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x608000283b60 h=--& v=--& MyApp.ViewA:0x7fe606511360.height == 0 (active)>", 
    "<NSLayoutConstraint:0x608000282030 UIView:0x7fe606511550.height == 42 (active)>", 
    "<NSLayoutConstraint:0x608000281d60 V:|-(0)-[UIView:0x7fe606511550] (active, names: '|':MyApp.ViewA:0x7fe606511360)>", 
    "<NSLayoutConstraint:0x608000281ef0 UIView:0x7fe606511550.bottom == MyApp.ViewA:0x7fe606511360.bottom (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000282030 UIView:0x7fe606511550.height == 42 (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. 

ViewA有一個非常簡單的設置:

extension MessageViewController { 

    open override var canBecomeFirstResponder: Bool { 
     return true 
    } 

    open override var inputAccessoryView: UIView? { 
     return messageInputView 
    } 

} 

爲什麼ViewA他我從來沒有設置的ight突破了我設置的限制?

+0

@RuslanSerebriakov我已經更新了上面的代碼 – runmad

回答

1

我認爲解決的辦法是禁用面具自動調整大小爲ViewA

viewA.translatesAutoresizingMaskIntoConstraints = false 

既然你與約束鋪設出來。現在它試圖將您的ViewA的高度設置爲0,從而導致問題。

+0

我已經添加了一些更多的細節。這是一個'inputAccessoryView',但即使將其添加爲常規子視圖,也會導致寬度和垂直位置因高度而不明確。 – runmad

+0

所以它的意圖是viewA的高度爲0(就像因爲目前的情況它應該被隱藏)? –

+0

ViewA應該是其子視圖設置的大小。 – runmad

相關問題