---
title: "setBackgroundImage(_:for:barMetrics:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uisegmentedcontrol/setbackgroundimage(_:for:barmetrics:)"
---

# setBackgroundImage(_:for:barMetrics:)

Sets the background image for given state and bar metrics.

## Declaration

```swift
func setBackgroundImage(_ backgroundImage: UIImage?, for state: UIControl.State, barMetrics: UIBarMetrics)
```

## Parameters

- `backgroundImage`: The background image to use for state and barMetrics.
- `state`: A control state.
- `barMetrics`: Bar metrics.

## Discussion

Discussion If backgroundImage is an image that resizableImage(withCapInsets:) returns, the system calculates the cap widths from that information. If backgroundImage isn’t an image that resizableImage(withCapInsets:) returns, the system calculates the cap width by subtracting one from the image’s width, then dividing by 2. The system uses the cap widths as the margins for text placement. To adjust the margin, use the margin adjustment methods. Generally, specify a value for the normal state. The segmented control uses this state for other states that don’t have a custom value set. Similarly, when a property is dependent on the bar metrics, be sure to specify a value for UIBarMetrics.default. The segmented control respects properties for UIBarMetrics.compact only when the control is in smaller navigation and toolbars.

## See Also

### Customizing appearance

- [selectedSegmentTintColor](uikit/uisegmentedcontrol/selectedsegmenttintcolor.md)
- [backgroundImage(for:barMetrics:)](uikit/uisegmentedcontrol/backgroundimage(for:barmetrics:).md)
- [contentPositionAdjustment(forSegmentType:barMetrics:)](uikit/uisegmentedcontrol/contentpositionadjustment(forsegmenttype:barmetrics:).md)
- [setContentPositionAdjustment(_:forSegmentType:barMetrics:)](uikit/uisegmentedcontrol/setcontentpositionadjustment(_:forsegmenttype:barmetrics:).md)
- [UISegmentedControl.Segment](uikit/uisegmentedcontrol/segment.md)
- [dividerImage(forLeftSegmentState:rightSegmentState:barMetrics:)](uikit/uisegmentedcontrol/dividerimage(forleftsegmentstate:rightsegmentstate:barmetrics:).md)
- [setDividerImage(_:forLeftSegmentState:rightSegmentState:barMetrics:)](uikit/uisegmentedcontrol/setdividerimage(_:forleftsegmentstate:rightsegmentstate:barmetrics:).md)
- [titleTextAttributes(for:)](uikit/uisegmentedcontrol/titletextattributes(for:).md)
- [setTitleTextAttributes(_:for:)](uikit/uisegmentedcontrol/settitletextattributes(_:for:).md)
