1

WPF contains Hidden (hides the control, but reserves the space it occupies in the layout) and Collapsed (does not render the control and does not reserve the whitespace)

Swift contains isHidden property only (myView.isHidden = true). How can I hide my control without whitespace?

Kirill
  • 1,412
  • 4
  • 21
  • 45
  • No, there is no way to do that. You would have to remove it from the parent. `view.removeFromSuperview()` – TheValyreanGroup Nov 22 '16 at 19:40
  • 1
    @TheValyreanGroup: No, your comment is not correct. There is a way to do this. @ kirill you can use UIStackView for achieving this – Midhun MP Nov 22 '16 at 19:45
  • 1
    May be this tutorial help you to understand about UIStackView usage: https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views – Midhun MP Nov 22 '16 at 19:47
  • @Midhun MP , thanks for the link! – Kirill Nov 22 '16 at 19:49
  • What about using a [UIStackView](https://developer.apple.com/reference/uikit/uistackview)? [this answer](http://stackoverflow.com/questions/40254889/uistackview-distribution-fill-equally/40256540#40256540) might be helpful. – Ahmad F Nov 23 '16 at 17:17

1 Answers1

1

If you're using storyboards and constraints, one clever way that I've found is to set the width or height constraint of the disappearing view to 0.

As an example:

a special alt-text present for you, curious reader!  it's beer!

@IBAction func onTapSquare(_ sender: Any) {
  let constraint = disappearingView.constraintForIdentifier(id: "example_width")
  constraint?.constant = 0
}

Note that you have to write the constraintForIdentifier function yourself, you can copy/paste from my view extension here: Github link!

I whipped up a tiny example project which you can grab here: Disappearing Constraint Example

If you've got margin in between the views, you can set that to 0 with a similar method. Good luck!

Here it is in action: gif of it in action

Elliot Fiske
  • 1,158
  • 8
  • 15