0
votes

I know how to accomplish this in something like sprite kit but with just a normal ViewController I'm a little lost - right now I have my button and my UITextfield set up on my xib file. Right now, when the button is clicked my textfield goes from hidden to not, which is great.

Problem is I need to have my button slide up (as in animate up) to "reveal" the text field when clicked. As in the textfield would be underneath the button. When another button is clicked, I want the button to slide back down to its original position. I don't know how to implement these animations.

Here is an image of what I need:

enter image description here

So the blue button would trigger the move up when clicked. Then if another button is pressed the blue button moves back to original position over textfield.

How can I accomplish this?

1
Will it always be 4 sections like this, or could there be a variable amount of sections?Wyatt
Always 4 - not variableskyguy

1 Answers

1
votes

Here's an option you can try if you aren't displaying these in a UITableView:

  1. Setup your .xib with all of the possible views in an 'expanded' state. Give your views that will be revealed a height constraint (in addition to the others needed to layout the view properly), and then create an outlet for those height constraints in your view controller. Additionally, store a reference to what you want the expanded height of the view to be in your view controller.
  2. In viewDidLoad of your view controller you will set those height constraints' constant values to 0. This will cause these views to effectively disappear, and your other views should fill in the gaps they leave behind based on their constraints.
  3. When one of your buttons is pressed, you'll update which views should be visible by setting their height constraint to 0 if you want them hidden, or to the value you saved earlier if you want them shown. After the height has been updated, call

    UIView.animateWithDuration(howLongTheAnimationLasts) {
        self.view.layoutIfNeeded()
    }
    

    which should animate the change in height values.

Alternatively, you could try putting your options in a UITableView and altering their height using tableView:heightForRowAtIndexPath: and reloading the cells when their heights should change.