如何在ScrollView中展开和折叠UIView?

时间:2018-05-23 10:24:20

标签: ios swift uiview uiscrollview

我在弹出窗口viewController中创建了一个弹出窗口,其中有一个scrollview,我在button click上有一些视图。我想要折叠包含一些视图本身和另一个button click的视图。我想扩展视图。我已经尝试了下面的代码实现它,但不是折叠视图,而是想隐藏其他视图。

崩溃我用过以下代码:

        var newFrame : CGRect = view.frame
        newFrame.size.height = 0

        UIView.animate(withDuration: 0.25, animations:{
            self.issueBox.frame = newFrame;
            self.issueBox.isHidden = true

        },completion : nil)

用于展开:

            var newFrame : CGRect = view.frame
        newFrame.size.height = 232

        UIView.animate(withDuration: 0.25, animations:{
            self.issueBox.frame = newFrame;
            self.issueBox.isHidden = false

        },completion : nil)

同样在我的故事板中,我为所有视图设置了约束。包括我要崩溃的那个。

那么如何折叠和展开视图呢?  这是我在viewController中的视图: red box is the one I want to collapse

我想在点击“折叠/延伸”按钮时折叠红色框,然后“确定”按钮,显示为“折叠/展开”按钮!

1 个答案:

答案 0 :(得分:1)

为要折叠的视图的高度约束设置动画(在0和所有子视图的计算高度之间切换)。确保其子视图不具有top | height | bottom约束,因为这将导致布局问题 - 添加top& amp;高度或底部&相反,对子视图的高度限制。确保您制作动画的视图已将Clip to Bounds设置为true。遇到约束时,请避免使用框架动画。希望有所帮助!