UIBackgroundConfiguration
A configuration that describes a specific background appearance.
Declaration
struct UIBackgroundConfigurationOverview
Background configurations provide a lightweight way for you to create backgrounds for your views. Using a background configuration, you can obtain system default background styling for a variety of different view states. You apply background configurations directly to UIButton or to cells, headers, and footers in UICollectionView and UITableView.
To use a background configuration:
Create a background configuration with one of the default system styles.
Modify the configuration to match your view’s style if you need additional customization.
Set the view’s current background configuration to your configuration.
var backgroundConfig = UIBackgroundConfiguration.listPlainCell()
// Set a nil background color to use the view's tint color.
backgroundConfig.backgroundColor = nil
cell.backgroundConfiguration = backgroundConfigYou can also start by creating an empty background configuration using clear(), which produces a transparent background.
Each of the system background styles provides system default values for different configuration states (UIConfigurationState). If you apply a background configuration to a view whose automaticallyUpdatesBackgroundConfiguration property is true, the system automatically updates the background configuration when the view’s state changes.
If you want additional customization beyond the system default values, you can choose to manually update the background configuration by overriding the view’s updateConfiguration(using:) method.
override func updateConfiguration(using state: UIConfigurationState) {
// Get the system default background configuration for a plain style list cell in the current state.
var backgroundConfig = UIBackgroundConfiguration.listPlainCell().updated(for: state)
// Customize the background color to use the tint color when the cell is highlighted or selected.
if state.isHighlighted || state.isSelected {
backgroundConfig.backgroundColor = nil
}
// Apply the background configuration to the cell.
self.backgroundConfiguration = backgroundConfig
} When you apply a configuration to a view, UIKit performs the actual drawing and rendering of the background. When you use background configurations instead of rendering your own backgrounds, the system provides automatic view hierarchy management, support for interactive and interruptible animations and transitions, and performance optimizations.
Topics
Creating cell background configurations
Creating header and footer background configurations
Creating an empty background configuration
Customizing the background
customViewcornerRadiusbackgroundInsetsedgesAddingLayoutMarginsToBackgroundInsetsbackgroundColorbackgroundColorTransformerresolvedBackgroundColor(for:)visualEffectshadowPropertiesUIShadowPropertiesstrokeColorstrokeColorTransformerresolvedStrokeColor(for:)strokeWidthstrokeOutsetimageimageContentMode