SwiftUI provides a modern, declarative way to build user interfaces on Apple platforms. While simple views are easy to create, developers often face practical challenges such as setting background colors correctly, managing layouts, reusing colors from the asset catalog, and displaying a footer that remains visible across multiple screens.
This article offers a complete, real-world guide to these common SwiftUI and UIKit integration scenarios.
Understanding Background Colors in SwiftUI
In SwiftUI, background colors are applied using the .background() modifier. Unlike UIKit, the background does not automatically fill the entire screen unless explicitly instructed.
Basic Background Color Example
Text("Hello SwiftUI")
.padding()
.background(Color.blue)
This applies the background only to the padded area of the text.
Full-Screen Background Colors
To set a background color that fills the entire screen, you must expand the parent container.
VStack {
Text("Content")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
Ignoring Safe Areas
If you want the background to extend under the status bar or home indicator:
.background(Color.black.ignoresSafeArea())
SwiftUI Layouts and Background Behavior
SwiftUI layouts (such as VStack, HStack, and ZStack) define how backgrounds behave.
ZStack for Layered Backgrounds
ZStack {
Color.green.ignoresSafeArea()
VStack {
Text("Foreground Content")
}
}
ZStack is ideal when your background is independent of content size.
Using Colors from the Asset Catalog in SwiftUI
Apple strongly recommends defining reusable colors in Assets.xcassets.
Step 1: Define a Color Asset
- Open Assets.xcassets
- Add a Color Set
- Name it (e.g.,
Greyscale50) - Define light/dark variants if needed
Step 2: Use the Color in SwiftUI
Color("Greyscale50")
SwiftUI automatically adapts to light and dark mode if configured in assets.
Using UIColor from Asset Catalog (UIKit & SwiftUI Interoperability)
When working with UIKit components (such as UITableView, UISearchBar, or UIViewController) alongside SwiftUI, you may need UIColor.
let customColor = UIColor(named: "Greyscale50")
Example: Applying Asset Color to UIKit Views
view.backgroundColor = UIColor(named: "Greyscale50")
tableView.backgroundColor = UIColor(named: "Greyscale50")
Applying Background Colors to Search Bars
UIKit search bars require additional configuration to fully override their default appearance.
searchBar.searchTextField.backgroundColor = UIColor(named: "Greyscale50")
searchBar.barTintColor = UIColor(named: "Greyscale50")
searchBar.backgroundImage = UIImage()
This ensures a consistent custom background color.
Creating a Footer Visible on All Screens (SwiftUI)
SwiftUI does not have a native “global footer,” but this can be achieved with architectural patterns.
Option 1: Wrapper Layout (Recommended)
struct AppContainer<Content: View>: View {
let content: Content
var body: some View {
VStack(spacing: 0) {
content
Divider()
FooterView()
}
}
}
Usage:
AppContainer {
MainView()
}
This ensures the footer appears on all screens using the container.
Creating a Footer Visible on All Screens (Storyboard / UIKit)
In UIKit with Storyboards, the most reliable solution is refactorization using container controllers.
Recommended Approaches
1. UITabBarController
- Footer stays persistent
- Best for navigation-driven apps
2. UINavigationController + Toolbar
- Toolbar acts as a footer
- Visible across all pushed screens
3. Custom Container View Controller
- Root controller embeds content + footer
- Most flexible solution
✅ This approach fixes the problem once through refactorization, rather than repeating footer code in every screen.
SwiftUI vs UIKit: When to Combine Them
| Feature | SwiftUI | UIKit |
|---|---|---|
| Background handling | Declarative | Imperative |
| Asset color usage | Color("name") | UIColor(named:) |
| Persistent footers | Layout-based | Controller-based |
| Search bar styling | Limited | Full control |
SwiftUI works best when combined with UIKit where fine-grained control is required.
Best Practices
- Define all colors in Assets
- Use
ZStackfor full-screen backgrounds - Prefer container views for global UI elements
- Avoid duplicating footers per screen
- Centralize layout decisions early in the project
Conclusion
Managing background colors, layouts, and global UI components in SwiftUI becomes straightforward once the correct architectural patterns are applied. By leveraging asset colors, container layouts, and controlled UIKit integration, you can achieve a clean, scalable UI that behaves consistently across all screens.
A well-planned structure solves the problem once through refactorization, saving time and reducing maintenance in the long run.


