简体   繁体   English

UIView的高度限制打破了子视图的高度限制

[英]UIView's height constraints break child view's height constraint

I have a really simple setup that I can't get to do what I want. 我有一个非常简单的设置,我无法做我想做的事情。

I'm trying to add an inputAccessoryView . 我正在尝试添加一个inputAccessoryView

For the inputAccessoryView I have a UIView ( ViewA ) with a subview ( ViewB ). 对于inputAccessoryView我有一个UIView (ViewA)与子视图(ViewB)。 ViewB is fixed to all edges of ViewA . ViewB固定在ViewA的所有边缘。 ViewB has a fixed height, which needs to be the height of both ViewA and ViewB . ViewB具有固定的高度,该高度必须同时是ViewAViewB的高度。

ViewA 's isn't set by me, but is 0 which breaks the height of ViewB (set to 42). ViewA的不是我设置的,而是0 ,这会破坏ViewB的高度(设置为42)。

ViewA is added as a subview on a UIViewController 's view and fixed to the bottom and both the leading and trailing edges (so width of ViewA and ViewB should be that of the UIViewController's view, which it is). ViewA作为子视图添加到UIViewController的视图上,并固定在底部以及前端和后端(因此ViewAViewB的宽度应该是UIViewController的视图的宽度)。

Here's the code I have: 这是我的代码:

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
}

Which results in the following error: 导致以下错误:

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 has a pretty simple setup: ViewA的设置非常简单:

extension MessageViewController {

    open override var canBecomeFirstResponder: Bool {
        return true
    }

    open override var inputAccessoryView: UIView? {
        return messageInputView
    }

}

Why does ViewA height that I never set break the constraint that I setup? 为什么我从未设置的ViewA高度打破了我设置的约束?

I think the solution is to disable the Autoresizing Mask for ViewA : 我认为解决方案是为ViewA禁用Autoresizing Mask:

viewA.translatesAutoresizingMaskIntoConstraints = false

Since you are laying it out with the constraints. 由于您要在约束条件下进行布局。 Now it tries to set your ViewA's height to 0 which causes the problem. 现在,它尝试将ViewA的高度设置为0,这会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改UIView约束高度会带来其子视图的约束警告 - Change UIView constraint height brings its subview's constraints warning 当UIView高度设置为零时,视图的子控件不会隐藏 - When UIView height set zero, view's child control not hide 在没有高度限制的情况下使用自动布局对视图的高度进行动画处理 - Animating a view's height with autolayout when there's no height constraint 根据视图的高度添加约束iOS - Add constraints based on view's height iOS UIImageView高度不是自动布局约束后子图像高度的高度。 - UIImageView height not height of child image's height after autolayout constraints. 使用约束基于内部视图高度设置UIView高度 - Set UIView height based on inner view height using constraints 约束不明确地表明表格视图单元格的内容视图的高度为零 - Constraints ambiguously suggest a height of zero for tableview cell's content view UIView的高度约束动画 - Height constraint animation of UIView 将Dynamic Cell的表视图内容高度设置为表视图高度约束ios - Set Dynamic Cell's Table View Content height to table view height constraints ios 根据UIScrollView的高度设置UIView的高度 - Setting a UIView's height based on UIScrollView's height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM