Search code examples
swiftviewexpandable

how can I expandable a view in swift


Im new in swift. In android, I using fragment to add or remove childview from parent view. I would like to expand an view when I switch a switch to on, and to collapse when I switch the switch to off. At the same time all the other elements I have in my design have to move down or up.

And I dont want to use Expandable table view.

Here is how the layout looks like when the switches are OFF enter image description here

And here is how the layout looks like when the switches are ON: enter image description here

I dont know how to make it, please help me.

I change height of collection view to 0, but position of other under view not change.

UIView.animateWithDuration(0.3, delay: 0.0, options:UIViewAnimationOptions.TransitionNone, animations: { () -> Void in

        if(!self.temp)
        {
            self.collection_seat.frame.size.height = 0
            self.temp = true
        }
        else
        {

            self.collection_seat.frame.size.height = 128

            self.temp = false
        }
        }, completion: { (finished: Bool) -> Void in

    })

Solution

  • Make the view you want to open to inside a view and use autolayout constraints as if it was open. Then after you've made it, make the height constraint of the containing view 0. When the switch turns on, expand the height constraint to whatever size it is supposed to be.