2

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?

Community
  • 1
  • 1
sch
  • 1,368
  • 3
  • 19
  • 35
  • Try setting the button alpha to 0 instead of hiding the button. – David H Oct 19 '19 at 11:51
  • @DavidH this works somewhat, I don't get any debugger warnings/conflicts, but the stackview will always have the height of the buttons, I want the other view to strech to the full height of my superview if all buttons are hidden – sch Oct 20 '19 at 12:02
  • 1
    Add a padding view - I just did that. Give it constraints on size. When you want no buttons, remove them, then set the constraints on the padding view to what you want. You can leave the padding view in the stack view or add and remove it. – David H Oct 20 '19 at 12:49
  • @DavidH adding the padding view, and setting it's `heightConstraint` to `equalToConstant: 0` actually removes the debugger warnings, thank you! (still think it's super weird that it works..) – sch Oct 20 '19 at 18:16

1 Answers1

2

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) 
Iliya Kisliy
  • 241
  • 1
  • 5