Make a UIButton programmatically in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a UIButton programmatically in Swift means instantiating the button in code rather than using Interface Builder. This gives you full control over the button's appearance, layout, and behavior at runtime. The basic steps are to create a UIButton instance, configure its title, style, and target action, set Auto Layout constraints (or a frame), and add it to the view hierarchy. This approach is essential for dynamic UIs where buttons are created based on data or conditions.
Basic UIButton Creation
UIButton(type: .system) creates a standard system button with default styling. The frame sets its position and size. addTarget connects the button's tap event to a method.
UIButton with Auto Layout
Setting translatesAutoresizingMaskIntoConstraints = false is required when using Auto Layout programmatically. Without it, the system creates conflicting constraints from the frame.
Customizing Appearance
UIButton supports different configurations for different states (.normal, .highlighted, .disabled, .selected). Each state can have its own title, color, and image.
Button with Image
SF Symbols (UIImage(systemName:)) provide scalable icons. For iOS 15+, UIButton.Configuration is the modern API for complex button layouts.
UIButton.Configuration (iOS 15+)
UIButton.Configuration replaces much of the manual styling code. It supports subtitles, image placement, activity indicators, and dynamic content updates.
Handling Button Actions
iOS 14 introduced UIAction-based button handling, which eliminates the need for @objc methods and target-action boilerplate. For iOS 13 and earlier, use the traditional addTarget approach.
Creating Buttons Dynamically
Using UIStackView with dynamically created buttons is a common pattern for menus, option lists, and form actions. The tag property identifies which button was tapped.
Common Pitfalls
- Forgetting translatesAutoresizingMaskIntoConstraints: When using Auto Layout, you must set this to
false. Otherwise the button's frame-based position creates conflicting constraints and the layout breaks silently. - Not adding to view hierarchy: Calling
UIButton(type:)creates the button in memory. You must callview.addSubview(button)to make it visible. A common mistake is configuring the button but forgetting to add it. - Using .custom type without configuration:
UIButton(type: .custom)creates a button with no default styling — no highlight animation, no tint color. Use.systemfor standard behavior or explicitly configure all visual states for.custom. - Target-action selector typo: If the
#selectormethod name does not match an@objcmethod, the app crashes at runtime with "unrecognized selector sent to instance." Always verify the method signature. - Retaining self in closures: When using
UIActionclosures that captureself, use[weak self]to avoid retain cycles, especially in view controllers that may be dismissed.
Summary
- Create buttons with
UIButton(type: .system)and configure title, color, and action - Use Auto Layout with
translatesAutoresizingMaskIntoConstraints = falsefor responsive layouts - Use
UIButton.Configuration(iOS 15+) for modern button styling with subtitles and images - Use
UIAction(iOS 14+) for closure-based tap handling instead of target-action - Use
UIStackViewto layout dynamically created buttons - Always set
translatesAutoresizingMaskIntoConstraints = falsewhen using programmatic constraints
Related reading
- Make a UIButton programmatically in Swift
- Make a VStack fill the width of the screen in SwiftUI
- Make a VStack fill the width of the screen in SwiftUI
- Make an HTTP request with android
- Make REST API call in Swift
- Make REST API call in Swift
- Make UINavigationBar transparent
- Making a UITableView scroll when text field is selected
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.