Creating an Expandable Button in Swift
Published in
2 min readJun 14, 2020
We’re going to build a custom expandable button in Swift, like the one below:
First off, it’s important to understand the view structure that is necessary to implement a component like this one.
We will create a subclass of UIView that will be composed of 5 subviews like so:
- A
containerStackView
that will hold themenuButton
and theexpandedStackView
- A
menuButton
that, when tapped, will handle the expanding animation - An
expandedStackView
that will be shown or hidden when themenuButton
is tapped - A
dogButton
that will be part of theexpandedStackView
- A
catButton
that will also be part of theexpandedStackView
Creating our custom UIView
Let’s start by setting up our view, which we will call ButtonPanelView
. We will first create 3 instances of UIButton
and 2 instances of UIStackView
.
We will add dogButton
and catButton
as arranged subviews of expandedStackView
. We will then add expandedStackView
and menuButton
as arranged subviews of containerStackView
.