Images in segmentedControl segments do not draw properly

This is UIKit app, in Xcode 26.3 (but same issue in 16.4).

I create (in IB) a segmentedControl, with 2 segments.

I set the images that are stored in assets. They show properly in Xcode. But when running (26.1 simulator), they just show a black image.

In Xcode                                                                           On simulator at runtime

I've tried to set background to clear as well as tint, to no avail.

What am I missing ?

Answered by Claude31 in 900323022

Posted a bug report: FB24168788

Accepted Answer

Posted a bug report: FB24168788

Hi @Claude31,

Does this occur on the latest 27 beta release?

When I put an an image in IB in a segmentedControl, I don't see the black square, but maybe I don't have the same code as you. I'm testing on iOS 27 simulator.

Can you add this information as well as a minimal sample project to your feedback report?

Thank you.

 Travis

Tested on latest 27 beta release, iOS 27.0 simulator.

Same issue.

I have attached a minimal project in FB24168788.

Just the template iOS project with UIKit and a segmentedControl with the images on the initial view.

Sorry for the insanely large images, that's a forum behaviour (bug introduced some time ago) I don't know how to correct.

- SegmentedControl definition in IB:

- Seen in IB :

- Seen in simulator:

PS: opening app in simulator with Xcode 27 takes about 30s, even with a very very minimal project. What do I miss ?

PS: app launches, but I get the following error in console:

non-launching port is incompatible with service identifier "com.apple.PointerUI.pointeruid.default-service"

objc[1809]: Class _UIKeyboardIntelligenceElement is implemented in both /private/var/run/com.apple.security.cryptexd/mnt/com.apple.iPhoneOS.SimulatorRuntime-v24.1.5390.6.lcUVso/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 27.0.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x1f03e1468) and /private/var/run/com.apple.security.cryptexd/mnt/com.apple.iPhoneOS.SimulatorRuntime-v24.1.5390.6.lcUVso/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 27.0.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AutoFillUI.framework/AutoFillUI (0x1045b8560). This may cause spurious casting failures and mysterious crashes. One of the duplicates must be removed or renamed.

How is it ?

Thanks! This is great.

After taking a look I see what's going on clearly now.

.alwaysOriginal always draws the original image, without treating it as a template

I was using a system image, which always draws as a template, without rendering as an original.

In your case, your image is a full-color PNG with the default rendering mode. Therefore it is expected.

This is documented in UIImage.h

navigation bars, tab bars, toolbars, and segmented controls automatically treat their foreground images as templates

Because no rendering mode is set, the image defaults to UIImageRenderingModeAutomatic, and UISegmentedControl renders it as a template filled with the tint color.

This appears to have been consistent for quite some time. I will share this information along with your report FB24168788, maybe this information will be helpful to the relevant team that report lands on.

If this was not documented or clear, maybe we should file a report to update the documentation so that others find this solution. If this is case, share the documentation you were using.

Thanks,

 Travis

@Travis, thanks. Effectively, it works with a systemImage.

So I should set to template.

But can I do this in IB or do I have to do it in code ?

I tried this, to no avail, still a black image:

        let flag = UIImage(named: "Flags")?.withRenderingMode(.alwaysTemplate)
        testSegmentedControl.setImage(flag, forSegmentAt: 0)
        testSegmentedControl.setImage(flag, forSegmentAt: 1)
        print(flag, flag?.renderingMode)

renderingMode is defined as print shows:

Optional(<UIImage:0x60000300c870 anonymous {24, 16} 
renderingMode=alwaysTemplate>) Optional(__C.UIImageRenderingMode)

Got it: it has to be .alwaysOriginal. Then it works

I do not see it possible in IB:

Got it; I can set the mode of the image in xcassets.

Images in segmentedControl segments do not draw properly
 
 
Q