How do you create a UIImage View Programmatically - Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a UIImageView in code is a standard UIKit task when you want full control over layout, lifecycle, and reuse behavior. Programmatic setup is often clearer than storyboard wiring for reusable components and custom screens. A solid implementation includes initialization, constraints, content mode, async image loading, and accessibility.
Create the Image View in Code
Start with a deterministic UIKit setup: create the view, configure it, disable autoresizing-mask translation, and add constraints.
This gives you a fully working image view without Interface Builder.
Pick the Right Content Mode
contentMode changes how the image is rendered inside the view bounds:
- '
.scaleAspectFitkeeps the entire image visible' - '
.scaleAspectFillfills the bounds and may crop' - '
.centerkeeps the original size and centers the image'
For avatars, .scaleAspectFill is often the right choice. For diagrams, QR codes, or screenshots, .scaleAspectFit is usually safer because cropping can remove meaningful content.
Choosing the wrong content mode is one of the most common reasons an otherwise correct UIImageView looks wrong.
Load Remote Images Asynchronously
If the image comes from the network, never fetch it synchronously on the main thread. Load in the background and update the UI on the main thread.
That pattern keeps scrolling and touch interactions responsive while the image is loading.
Add Caching for Reuse
If the same images are shown repeatedly, a simple in-memory cache reduces duplicate network and decoding work.
This is especially important when image views live inside table-view or collection-view cells.
Handle Reuse and Accessibility
If the image view is part of a reusable cell, reset state in prepareForReuse so stale images do not flash during scrolling.
Also add accessibility metadata when the image conveys meaning:
Programmatic UI setup should include semantics, not just pixels.
UIKit First, Even in Mixed SwiftUI Projects
The article title is about Swift, but this is fundamentally a UIKit task. Even in apps that use SwiftUI heavily, programmatic UIImageView setup remains useful in legacy screens, wrappers, and custom components.
That means the same discipline still applies:
- explicit layout
- explicit async loading behavior
- explicit reuse handling
The framework wrapper may change, but the view lifecycle concerns do not.
Common Pitfalls
The biggest pitfall is forgetting translatesAutoresizingMaskIntoConstraints = false before activating Auto Layout constraints.
Another common issue is setting the wrong contentMode, which causes unexpected stretching or cropping.
People also update the image view off the main thread after a network response, which can lead to UI bugs and hard-to-reproduce behavior.
Summary
- Create
UIImageViewprogrammatically with explicit configuration and constraints. - Choose
contentModebased on the actual image semantics. - Load remote images asynchronously and update the UI on the main thread.
- Add caching and reuse handling for list-based interfaces.
- Include accessibility metadata so the image view is usable, not just visible.
Related reading
- How do you create custom notifications in Swift 3?
- How do you debug React Native?
- How do you debug React Native?
- How do you dismiss the keyboard when editing a UITextField
- How do you display a Toast from a background thread on Android?
- How do you document the parameters of a function's closure parameter in Swift 3?
- How do you draw a line programmatically from a view controller?
- How do you dynamically add elements to a ListView on Android?
.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.