2017-12-02 223 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

相关问题