Change UISegmentedControl selected index or value programmatically

swift segmented control programmatically
uisegmentedcontrol set selected segment programmatically
uisegmentedcontrol subviews
uisegmentedcontrol customize appearance
custom segmented control swift 4
uisegmentedcontrolnosegment
uisegmentedcontrol change segment programmatically
ios 13 segmented control

I have a UISegmentedControl with two segments (index: 0 and 1) that I am trying to reset programmatically to 0. When I run the command below it does not reset the segment index to 0 as expected. Instead it highlights segment indexed 1.

    [seg setSelectedSegmentIndex:0];

Oddly when I log the selected segment, it returns 0.

    NSLog(@"seg setSelectedSegmentIndex %d", seg.selectedSegmentIndex); 

Also oddly, after running setSelectedSegmentIndex:0 I cannot reselect segment 0 manually by touch, it seems to be locked to segment 1, until I tap 1 wherein it works as normal.

Here is the setup for the button:

    itemArray = [NSMutableArray arrayWithObjects: @"Zero", @"One", nil];
    seg = [[UISegmentedControl alloc] initWithItems:itemArray];
    [seg setFrame:segRect];
    seg.segmentedControlStyle = UISegmentedControlStyleBar;
    seg.momentary = NO;
    [seg addTarget:self action:@selector(someAction:) forControlEvents: UIControlEventValueChanged];
    [mainView addSubview:seg];

NOTE: To restate what I was trying to accomplish with this question: I want to set a UISegmentedControl's selected index via a variable dynamically, get the appropriate visual feedback, and check the selected index in a method, to make something happen depending on the selected index. See my answer below for a solution.

Change in viewDidLoad:

[seg setSelectedSegmentIndex:index];

you change use the index value as your wish.

Create UISegmentedControl in Swift Programmatically, How to create UISegmentedControl in Swift programmatically; How to add when UISegmentedControl value is changed; Change UISegmentedControl tint for each selected segmented control index from a remote server side php script . 2. How To Add iOS Segmented Control Component Programmatically Steps. Click to edit the ViewController.swift file.; Then copy below code into it. The code contains how to create the second segmented control, how to add both text and image items into it, how to add layout constraints to it, how to process it’s value changed events etc.

From code, you can just do seg.selectedSegmentIndex = someDefaultIndex.

Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.

selectedSegmentIndex, The index number identifying the selected segment (that is, the last segment The default value is UISegmentedControlNoSegment (no segment selected) until UISegmentedControl ignores this property when the control is in momentary mode. When the user touches a segment to change the selection, the control event� In this Swift code snipped you will learn: How to create UISegmentedControl in Swift programmatically How to add UISegmentedControl as a Subview Add custom function which gets triggered when UISegmentedControl value is changed Change UISegmentedControl tint colour Change UISegmentedControl background colour import UIKit class ViewController: UIViewController { override func viewDidLoad

Swift 3.0

segmentedControl.selectedSegmentIndex = #your value#

UISegmentedControl, In versions of iOS prior to 3.0, if a segmented control has only two segments, then it of a property to be used by other states which don't have a custom value set. The index number identifying the selected segment (that is, the last segment� The selectedIndex property sets or returns the index of the selected option in a drop-down list. The index starts at 0. Note: If the drop-down list allows multiple selections it will only return the index of the first option selected. Note: The value "-1" will deselect all options (if any).

This helps sort the selected segment, in terms of the visual feedbak, that is after programatically setting the selected segment this will tint the segments properly.

for (int i=0; i<[seg.subviews count]; i++)
{

    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[seg.subviews objectAtIndex:i]isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor grayColor]];
    }
    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[seg.subviews objectAtIndex:i] isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
    }
}

How to customize UISegmentedControl without losing your mind , This one is just the very basic segmented control with a white tint color, and we programmatically Also, setting a selected segment index to start with is often a good At the bottom of the file where the controller is created, replace that It is also worth noting that it is possible to set a per segment width. This property indicates the zero-based index of the currently selected item in the combo box list. Setting a new index raises the SelectedIndexChanged event. SelectedIndex, SelectedValue, and FormattingEnabled are related as follows: If FormattingEnabled is false, SelectedIndex will not be set to -1 when SelectedValue is blank.

The correct answer is the @arcady bob one at this link. I have posted an updated version for Swift 5, which I quote here:

yourSegmentedControl.selectedSegmentIndex = 0

yourSegmentedControl.sendActions(for: UIControl.Event.valueChanged)

If you use just the first line, the segmented will react accordingly graphically, but your IBAction associated with the segmented control won't be called. In a few words: the second line is like tapping on the segmented control. This is what I needed and what I was missing.

iOS 13 Segmented Control Design Problem: How To Solve It , My problem with iOS 13 was displaying Segmented Control. of the already mentioned changes that came with iOS 13: a selected tint The yellow line under my Segmented Control was added programmatically as subview. // It searches for all occurrences of the selected employee's // name in the Items array and adds the employee's name and // the number of occurrences to TextBox1.Text. // CAUTION This code exposes a known bug: If the index passed to the // FindStringExact(searchString, index) method is the last index // of the array, the code throws an exception.

How to programmatically add a UISegmentedControl to a container , To add a UISegmentControl in iOS with swift we'll have to first create a segment control and it's controller function, i.e. it's action. Let's see those� Programmatically changing selected index The selected index can be changed on the fly by assigning the selectedIndex prop to a state variable, then changing that variable. Note that the state variable would need to be updated as the user selects a value and changes the index, as shown in the example below.

Designing a Button Bar-Style UISegmentedControl in Swift , If not, the button bar at the bottom of the selected segment might end segmented control to an arbitrarily chosen value segmentedControl. To bring back the labels, let's change the font and text color of both the selected segment and the first index, but the selected segment stays “highlighted” properly. It's based on the way the combobox populated,if you bind the combobox with a datatable,and set its displayMember and ValueMember,then the selectedValue should work,as bob zhu's code shows.

Selection changed | SfSegmentedControl | Xamarin.iOS, How to handle selection changed in segmented control. The selection changed event occurs when changing from one segment item to another in the segmented Users can set the default value programmatically for selection to be placed. gridView1.Rows[index].Selected = true; but the record selector will continue pointing to the selected record in the underlying data source. To then make the record pointer point to the selected row you must also perform a. dataSource.Position = index; matching the record selected in the GridView control. You can also use the Move methods.

Comments
  • are both the segments enabled before you try to select? (using isEnabledForSegmentAtIndex)
  • @calvinBhai I think so, wouldn't the default be enabled?
  • I have the same issue, segment 0 cannot be reselected once one of the others have been. I am going to release with a workaround of having a blank seg 0 for now. My segment is inside a UIControl inside a UIScrollView. I had the problem previously when iOS 5 worked fine, but the same binary on iOS6 exhibited this issue.
  • @NickThorne yeah, seems like should be fixable. That is: to do programatically what happens when the view is tapped. I'll add it here if I figure it out.
  • i know it should be default, that was a "just in case". is the segment being added to the view in viewDidLoad or in the viewWill/DidAppear? (there is a good chance that setSelectedSegmentIndex will work better after the view is loaded)
  • Yes. That is what I am doing. Thanks.