Segment control not working in visionOS

Anybody else having a problem using the segment control in visionOS. My setup has 3 segments. But after I tap segment 2 or 3 then the next tap returns focus to segement 1 not matter what segment I actually tap and then no further taps on segment 1, 2 or 3 trigger the valuechanged event and it appears that segments 2 and 3 are disabled. To diagnose this I added a to the view but I did not wire it up to anything and got the same results so it not my code causing this behavior. Any ideas?

Answered by apgsolutionsllc in 785405022

The solution I found that works is to in code remove any existing segments at run time then add them back again. After that the segment control works as expected below is some sample code:

[self.segmentList removeAllSegments];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"currenttab", @"") atIndex:0 animated:NO];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"alltabs", @"") atIndex:1 animated:NO];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"favorites_title", @"") atIndex:2 animated:NO];

The say a picture is worth a thousand words

Step 1: the view loads with first tab selected by default:

Steps 2: select segment #2, I get the value did change event with selectedSegmentIndex is 1 and segment #2 gets highlighted

Step 3: select segment #3, I get the value did change even but selectedSegmentIndex is 0, the first segment gets highlighted but segment #3 should be highlighted

Step 4: If select any segment 1, 2 or 3 then no value changed event is triggered and focus remains on segment #1

Said another way, after selecting a second segment, the control returns focus to segment #1 and seems to be non responsive.

Hopefully the pictures and words paint a clearer picture.

Could you provide an example of the code you are using to accomplish this? In the Hello World sample code, the Objects In Orbit screen uses a Picker with three items and I do not see the behavior you are describing there.

My app use objective c not SwiftUI so the segment control is added to a view controller in interface builder and pinned to the top of the view controller with padding. The only code sets the title of the segments, sets the selected segment and listens to the value changed event.

To debug if it was a code issue or maybe to see if I wired the control incorrectly, I added another segment control to the view in interface builder with 3 segments but I didn't create an outlet to access the control in code and I didn't link it to any events and I got the same behavior.

I filed bug report 13678160 in the feedback assistant with a video attached, can you see this.

You can recreate this issue by

  1. Creating a new app project in Xcode, I am using Xcode 15.3:

  1. Set the interface to storyboard and the language to objective c

  1. In the default storyboard, add a segment control with 3 segments. In my test project I added 2 segment controls one pined/anchored to the top of the view and the second control with not anchoring to see if it made a difference. No need to wire up any outlets

  1. Run the project using Apple Vision Pro target in the simulator on an actual device.

  1. Select the second or third segment, then select any other segment. That will cause focus to return to the first segment and no other taps/click/pinches will have any effect

Thanks for filing a feedback request and including a sample project. As noted within the issue navigator in Xcode, compiling Interface Builder products for visionOS will not be supported in a future version of Xcode. Additionally, the UIStoryboard documentation highlights that for visionOS apps, you can load existing storyboards, but you can’t add content specific to the platform. UIStoryboard is deprecated on visionOS, please migrate your interface code to SwiftUI as soon as possible.

Accepted Answer

The solution I found that works is to in code remove any existing segments at run time then add them back again. After that the segment control works as expected below is some sample code:

[self.segmentList removeAllSegments];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"currenttab", @"") atIndex:0 animated:NO];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"alltabs", @"") atIndex:1 animated:NO];
[self.segmentList insertSegmentWithTitle:NSLocalizedString(@"favorites_title", @"") atIndex:2 animated:NO];
Segment control not working in visionOS
 
 
Q