- Start a blank, single view application
- Add a UIStackView to the main view controller with 4 UIView subviews.
- Set the stack views properties as follows:
- Axis: Horizontal
- Alignment: Fill
- Distribution: FillEqually
- Spacing: Whatever u want (But don't go crazy)
- Set different background colors for each UIView
- Constrain the stack view to the edges of the superview
- Set the stack views properties as follows:
- Add a UIButton anywhere (Mine is in the left-most view) with title: "Go vertical"
- Add references to the following views:
- Stack view (
stackView
) - Button - Reference (
axisButton
) - Button - Action (
axisButtonPressed
)- print something so we know the button is hooked up properly
- UIView on the right (Name it by its color not
lastView
orrightView
cause Spoiler Alert that's going to change)
- Stack view (
- Build and run, make sure it looks good.
- Make sure the button is hooked up properly by printing what you wrote.
- Create a new function called
toggleAxis
with one Bool parameter namedanimated
- In the
axisButtonPressed
call thetoggleAxis
func. - In the
toggleAxis
func, toggle the stackView's axis with the following lines:
if stackView.axis == .horizontal {
stackView.axis = UILayoutConstraintAxis.Vertical
} else {
stackView.axis = UILayoutConstraintAxis.Horizontal
}
-
Create a new function called
updateAxisButtonTitle
and add this line:let newTitle = stackView.axis == .horizontal ? "Go vertical" : "Go horizontal" axisButton.setTitle(newTitle, forState: .Normal)
-
Call
updateAxisButtonTitle
at the bottom oftoggleAxis
-
Build and run.
-
Press the axis button and watch the magic. (Flip from a Horizontal to a Vertical stack)
-
Stretch Clean the
toggleAxis
func up.
- Add a UISwitch somewhere and add an outlet connection
animatedSwitch
- Add the animated parameter to the
toggleAxis
call based on the animated switch status - Wrap the
toggleAxis
code in a UIView animation block - Call
updateAxisButtonTitle
in the completion - Build and run and watch the animation on the axis change. (oohs and ahhs)
- In the storyboard add a button (anywhere besides the last view).
- And add an outlet and action connect. (
toggleLastButton
, andtoggleLastButtonPressed
) - Add a function entitled
toggle{Color}View(hidden: Bool)
- In the function, set the (color)View hidden property to be equal to the hidden parameter
- Call
toggle(Color)View
from theremoveButtonPressed
action function - Build and run to watch the last view appear and reappear
- Add another UISwitch somewhere and add an outlet to it
- Wrap the
toggleColorView
in a UIView animation block based on the switch status. - Build and run to watch the animation of the last view getting removed/added
- Stretch Get the last view to fade as it animates out and fade back in as it comes back in.
- Stretch Get the last view to bounce in and out with a spring effect