Empty UIStackView causes ambiguous hieght/position

2.3k Views Asked by At

I have two views, one UIView and one UIStackView

let mainView = UIView()
mainView.translatesAutoresizingMaskIntoConstraints = false

let stackView = UIStackView()
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false

They are both added to the superView with these constraints

NSLayoutConstraint.activate([
    mainView.topAnchor.constraint(equalTo: view.topAnchor),
    mainView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    mainView.leadingAnchor.constraint(equalTo: view.leadingAnchor),

    stackView.topAnchor.constraint(equalTo: mainView.bottomAnchor),
    stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

When both the UIView and the UIStackView doesn't hold any other views, I get warnings on both views in the debugger

Height is ambiguous for uiview

Height and vertical position is ambiguous for uistackview

But if I add a UIButton to the UIStackView, and then set it to hidden with .isHidden = true, both warnings are gone, except for iOS10 where both warnings remain.

With or without the debugger warnings I get the result I want, that if the UIStackView is empty (or all subviews hidden), the UIView covers the whole screen.

What is going on here, why is one hidden view not the same as an empty stackView, and why does the behavior differ on iOS10? And last but not least, how can I satisfy iOS10 without compromising my current layout, that seemingly works for everything after iOS10?

1

There are 1 best solutions below

1
Iliya Kisliy On

Here's what you got — mainView and stackView are have no heights and edges are clipped. There's no way to calculate vertical position and height base on that constraints. You can add some height constraint on either view to make it clear for auto layout

mainView.heightAnchor.constraint(greaterThanOrEqualToConstant: 1)