隐藏一个元素并在 Swift 中占据它的 space (iOS)

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

我是 iOS 开发的新手,对我来说最困难的事情之一就是自动布局。我正在尝试创建一个简单的应用程序,它会显示一条消息,指出用户使用的 iOS 版本 运行 该应用程序尚未经过测试,因此可能会出现错误或意外情况,因为它是越狱应用

警告是一个简单的 UIView,带有两个标签和一个与 UIView 具有相同宽度和高度的按钮。 该警告需要 space 我想用于其他用途,因此当用户点击它时警告会隐藏。

我使用 self.unstableWarning.isHidden = true,但是正如预期的那样,它只隐藏了 UIView。

在警告下方,我有一个 UIScrollView,我想使用 space 警告,所以当隐藏警告时,我想要 UIScrollView 移动它也需要空白 space 离开。

这是我应用程序的一些代码:

@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)
    }

如何在 Swift 中实现此目的?

提前致谢

有多种方法可以做到这一点。我列举一下我能想到的:

  1. 使用stackView,你可能需要为警告视图设置一个恒定的高度。在此垂直 stackView 中,添加警告视图和滚动视图。如果警告视图被隐藏,滚动视图将自动占用整个堆栈视图。

  2. 在隐藏警告视图时操纵 scrollView 顶部约束。

  3. 将scrollView的顶部添加到警告视图的底部,并将警告视图的alpha设置为0并根据需要将其向上移动。