amodrono
amodrono

Reputation: 2040

Hide an element and take up its space in Swift (iOS)

I'm new into iOS development and one of the most difficult things for me is all the Auto Layout stuff. I'm trying to create a simple app that displays a message saying that the iOS version the user is running the app on hasn't been tested so bugs or unexpected things might occur, as it is a Jailbroken app.

simple message I'd like to hide

The warning is a simple UIView with two labels and one button that has the same width and height of the UIView. The warning takes a space I'd like to use for other things, so the warning is hidden when the user taps on it.

I use self.unstableWarning.isHidden = true, but that, as expected, only hides the UIView.

Below the warning, I have a UIScrollView which I'd like to take the space the warning uses, so when the warning is hidden, I'd like the UIScrollView to move so It also takes the blank space left.

Here's some of my app's code:

@IBOutlet weak var unstableWarning: UIView!
@IBAction func errMessageButton(_ sender: UIButton) {
        let alert = UIAlertController(title: "Unstable build or iOS version", message: "This version of Thunderbolt (" + projectVersion + ") hasn't been tested on your platform/iOS version (" + UIDevice.current.systemVersion + "), or very little tested, so expect bugs or unexpected things to occur. Thanks for using Thunderbolt ;)", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            self.unstableWarning.fadeOut()
            self.unstableWarning.isHidden = true
        }))

        self.present(alert, animated: true)
    }

How can I achieve this in Swift?

Thanks in advance

Upvotes: 0

Views: 1707

Answers (1)

Glenn Posadas
Glenn Posadas

Reputation: 13300

There are multiple ways to do it. I'll enumerate what I can think of:

  1. Use stackView, you'll probably need to set a constant height for the warning view though for this. In this vertical stackView, add the warning view and the scrollView. Should the warning view be hidden, the scrollView will automatically take the whole stackView.

  2. Manipulate scrollView top constraint upon hiding the warning view.

  3. Add scrollView's top to warning view's bottom, and set warning view's alpha to 0 and move it above according to your desired position.

Upvotes: 3

Related Questions