1

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

    })
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Kul Ken
  • 163
  • 1
  • 2
  • 8

2 Answers2

1

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.

Sam
  • 956
  • 6
  • 12
  • I use this way but the other views under switch still in there, it not run down – Kul Ken Jul 14 '16 at 04:44
  • Are they properly auto constrained to the views above? Can you show us some code/screenshots of what you have done – Sam Jul 15 '16 at 04:31
  • I have edit my post and add some code. When height = 0: http://i.stack.imgur.com/lUECm.png and expand: http://i.stack.imgur.com/ONC3w.png constrain http://i.stack.imgur.com/SH2XV.png – Kul Ken Jul 18 '16 at 07:55
  • You are setting the frames height equal to zero, not the height constraint. Changing a views frame will break the constraints on it – Sam Jul 18 '16 at 23:45
  • 2
    Create an IBOutlet for the height constraint. Where you set frame.size.height = 0, instead change the IBOutlets .constant =0 – Sam Jul 20 '16 at 06:56
0

Better to keep things in Table View. Refer this example. Maintain plist file to expand and collapse.

http://www.appcoda.com/expandable-table-view/

Hasya
  • 9,792
  • 4
  • 31
  • 46