UISegmentedControl() constructor
The UISegmentedControl() constructor creates a segmented control, with one segment for each item in the array.
Syntax
new UISegmentedControl()
new UISegmentedControl(items)Parameters
items optional
An array whose items are strings (the segment's title) or UIImage objects (the segment's image), in any mix. Numbers and booleans are converted to strings.
Omitting it, passing null, or passing an empty array all produce a control with no segments.
Return value
A new UISegmentedControl.
numberOfSegmentsequals the number of items initems.selectedSegmentIndexis -1: no segment is selected. You usually write an initial value right after creating it.
Exceptions
TypeError
itemsis not an array:new UISegmentedControl needs an array of strings and UIImages, got "日周月".- An item is not a string, number, boolean, or
UIImage(for examplenull, aUIColor, or a plain object):new UISegmentedControl: items[1] must be a string or a UIImage, got null.
Examples
const seg = new UISegmentedControl(['Day', 'Week', 'Month']);
seg.selectedSegmentIndex = 0;
console.log(seg.numberOfSegments);Result: logs 3. The three segments are "Day", "Week", and "Month", and segment 0 is selected.
Mixing titles and icons
const star = UIImage.systemImageNamed('star.fill');
if (star !== null) {
const seg = new UISegmentedControl(['All', star]);
console.log(seg.titleForSegmentAtIndex(1));
}Result: logs null. Segment 1 is an image segment and has no title.
systemImageNamed returns null when the name does not exist, and putting null in the array throws, so check for null first.
Corresponding iOS API
-[UISegmentedControl initWithItems:]. new UISegmentedControl() corresponds to [[UISegmentedControl alloc] init].
See also
insertSegmentWithTitle(): adds segments after creation