Greg Robertson Greg Robertson - 2 months ago 41
Swift Question

VoiceOver with UISegmentedControl in Swift

I am trying to add VoiceOver to a Segmented Control but the Apple example code does not work with Swift 3:

The Objective C code:

NSString *title = @”∫”;
title.accessibilityLabel = @”Integral”;
[segmentedControl insertedSegmentedWithTitle:title];


does not work with Swift like this:

var title: NSString = "∫"
title.accessibilityLabel = "Integral"
segmentedControl.insertSegment(withTitle: title, at: 0, animated: false)


Swift will only accept a String but I need to add an NSString since it has the VoiceOver attributes.

Can any help with this?

Is there a better approach for adding accessibility or VoiceOver to a segmented control?

Thanks

Greg

Answer

In line with Andy's comment I coded:

    segmentedControl.subviews[0].accessibilityLabel = ”Seg 2”;
    segmentedControl.subviews[1].accessibilityLabel = "Seg 1"
    segmentedControl.subviews[2].accessibilityLabel = "Seg 0"

Not the best, note subviews are reverse to appearance, but appears to be the only way at this time.

Greg