Make a UIButton programmatically in Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a UIButton in code is useful when the UI is dynamic, when you want reusable view components, or when you prefer code-based layout over storyboards. The basic process is simple: create the button, configure its appearance, wire up an action, and add constraints so it appears where you expect.
Creating the Button
The most direct starting point is UIButton(type:). A system button gives you platform-consistent behavior and styling defaults.
At this point the button exists and responds to taps, but it still needs layout before it appears in a useful place.
Laying Out the Button with Auto Layout
If you create views in code, you almost always want Auto Layout instead of hard-coded frames. The important setup step is disabling the autoresizing-mask translation.
This is the pattern to remember: create, add as a subview, disable autoresizing translation, and then activate constraints.
Styling the Button
You can style a button with the older property-based API or the newer configuration API. For modern iOS work, UIButton.Configuration is often the cleaner choice.
This approach keeps visual settings together in one place and avoids manually coordinating title color, insets, background, and image spacing.
If you only need basic styling, the older API is still valid:
Choosing an Action Style
The traditional pattern uses addTarget and an @objc selector. That is still common and works across many UIKit codebases. If you are targeting newer iOS versions, UIAction can be a nice alternative for inline behavior.
For reusable controls inside larger view controllers, UIAction can make short button behavior easier to read. For more established UIKit patterns or older deployment targets, selectors remain perfectly fine.
Reusing Button Setup
If several screens use the same visual style, create a helper instead of duplicating configuration everywhere.
That keeps color, shape, and spacing consistent across the app.
Common Pitfalls
- Forgetting
view.addSubview(button), which means the button never enters the view hierarchy. - Adding constraints without setting
translatesAutoresizingMaskIntoConstraints = false. - Using
.touchDownwhen you really want standard tap behavior from.touchUpInside. - Mixing heavy manual styling with
UIButton.Configurationand then wondering which settings take effect. - Capturing too much view-controller state in a
UIActionclosure when a selector would be clearer.
Summary
- Create a button with
UIButton(type:), then configure title, appearance, and behavior. - Add it to the view hierarchy before expecting it to render.
- Use Auto Layout for predictable placement in code-based interfaces.
- '
UIButton.Configurationis a strong default for modern UIKit styling.' - Choose either selectors or
UIActionbased on the deployment target and the complexity of the interaction.

