Questions tagged [uisegmentedcontrol]

UISegmentedControl is a class from UIKit in Apple iOS framework. A UISegmentedControl object is a horizontal control made of multiple segments, each segment functioning as a discrete button.

A UISegmentedControl object is a horizontal control made of multiple segments, each segment functioning as a discrete button. A segmented control affords a compact means to group together a number of controls.

A segmented control can display a title (an NSString object) or an image (UIImage object). The UISegmentedControl object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.

You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below.

SWIFT

segmentedControl.addTarget(self, action: "action:", forControlEvents: .ValueChanged);

OBJECTIVE-C

[segmentedControl addTarget:self
                     action:@selector(action:)
           forControlEvents:UIControlEventValueChanged];

How you configure a segmented control can affect its display behavior:

If you set a segmented control to have a momentary style, a segment doesn’t show itself as selected (blue background) when the user touches it. The disclosure button is always momentary and doesn’t affect the actual selection.

In versions of iOS prior to 3.0, if a segmented control has only two segments, then it behaves like a switch—tapping the currently-selected segment causes the other segment to be selected. On iOS 3.0 and later, tapping the currently-selected segment does not cause the other segment to be selected.

Customizing Appearance In iOS v5.0 and later, you can customize the appearance of segmented controls using the methods listed in Customizing Appearance. You can customize the appearance of all segmented controls using the appearance proxy (for example, [UISegmentedControl appearance]), or just of a single control.

When customizing appearance, in general, you should specify a value for the normal state of a property to be used by other states which don’t have a custom value set. Similarly, when a property is dependent on the bar metrics (on the iPhone in landscape orientation, bars have a different height from standard), you should make sure you specify a value for UIBarMetricsDefault.

In the case of the segmented control, appearance properties for UIBarMetricsLandscapePhone are only respected for segmented controls in the smaller navigation and toolbars that are used in landscape orientation on the iPhone.

To provide complete customization, you need to provide divider images for different state combinations, using setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics::

SWIFT

// Image between two unselected segments.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
    rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

// Image between segment selected on the left and unselected on the right.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Selected,
    rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

// Image between segment selected on the right and unselected on the left.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
    rightSegmentState: UIControlState.Selected, barMetrics: UIBarMetrics.Default)

OBJECTIVE-C

// Image between two unselected segments.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                  rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the left and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateSelected
                  rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the right and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                  rightSegmentState:UIControlStateSelected barMetrics:barMetrics];

For more information about appearance and behavior configuration, see Segmented Controls.

1825 questions
19
votes
6 answers

How to add the UISegmentedControl in UINavigationBar?

I have tried to add the UISegmentedControl to the bottom of UINavigationBar with title. But i cannot add it and I cannot add UISegmentedControl in the tableView.headerView,because i need the search bar in the tableView.headerView, so there is just…
Jacky Shek
  • 953
  • 1
  • 11
  • 35
18
votes
6 answers

How to display only bottom border for selected item in UISegmentedControl?

I'm extremely new to iOS development and ran into some trouble while building an app for a course. I created a segmented control and its init function (shown below) is being called in the view controller class containing the segmented control. I was…
Richa Netto
  • 305
  • 2
  • 4
  • 8
18
votes
5 answers

How to set image to the UISegmentedControl in iPhone?

I am new to iPhone development. I have created UISegmentedControl having 2 segments. I want to display images for each segment instead of title. Here is my code NSArray *itemArray = [NSArray arrayWithObjects: @"segment1", @"segment2",…
Warrior
  • 39,156
  • 44
  • 139
  • 214
18
votes
4 answers

UISegmentedControl bounds

I want to give the following aspect to an UISegmentedControl: Note the gray background view, and the white background of the segmented control non selected item. But, if I give a white background to my UISegmentedControl, I get the following: Note…
neutrino
  • 2,297
  • 4
  • 20
  • 28
18
votes
7 answers

Change UISegmentedControl selected index or value programmatically

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…
OWolf
  • 5,012
  • 15
  • 58
  • 93
17
votes
6 answers

UISegmentedControl in iOS 7 divider image is wrong during animation

I have a custom UISegmentedControl. In iOS 6 and bellow it works fine. Under iOS 7.. it looks fine until I press the control, at which time, the divider image looks weird for a split second. Here is my code: UIImage *segmentSelected = [[UIImage…
Fervus
  • 710
  • 7
  • 18
16
votes
4 answers

Make a segment of a segmented control invisible

Is it possible to make one segment of a segmented control invisible?
Xavi Valero
  • 2,047
  • 7
  • 42
  • 80
16
votes
5 answers

Change width of a segmented control based on titles in each segment?

Starts like this, which I like: But then I add a segment and this happens: The width is set in IB, not in code. All I need is a method to calculate width on the fly. At the end, it would do something like this: control.width = (labelWidths +…
Thromordyn
  • 1,581
  • 4
  • 17
  • 45
15
votes
3 answers

Deleselect uisegmentedcontrol selection after clicking on a save button

I'm using a 3 button UISegmented control for a choice selection. I also have a save button that retrieves the chosen control. When the save button is clicked I want to have to UISegmentedcontrol cleared (ie the previous selected button unselected).…
Ayrad
  • 3,996
  • 8
  • 45
  • 86
15
votes
2 answers

UISegmentedControl delegate/Touch Events

I have a UISegmentedControl that has six segments, I want them to call a method when the value changes, but also when each segment gets a UIControlEventTouchDragIn because I want a UILabel to show up with it's name when the person is trying to…
Jab
  • 26,853
  • 21
  • 75
  • 114
15
votes
1 answer

UISegmentedControl, UIToolbar and UINavigationItem

I've create a UISegmentedControl and successfully attached it to my navigationItem.tableView. But when I try instead to attach it to a UIToolbar, it blows up. I'm sure I've seen UISegementedControls on toolbars before - but can't seem to get it…
Luther Baker
  • 7,236
  • 13
  • 46
  • 64
15
votes
9 answers

Use iOS 6 Style Segmented Control in iOS 7?

Is it possible to have a segmented control on an iOS 7 device show up as the iOS 6 version of the control? We really aren't ready for an interface redesign and the new flat control doesn't jive with the rest of our UI. It would definitely be best…
MikeS
  • 3,891
  • 6
  • 35
  • 51
15
votes
9 answers

Why is the top portion of my UISegmentedControl not tappable?

While I was playing on my phone, I noticed that my UISegmentedControl was not very responsive. It would take 2 or more tries to make my taps register. So I decided to run my app in Simulator to more precisely probe what was wrong. By clicking dozens…
Pwner
  • 3,714
  • 6
  • 41
  • 67
15
votes
2 answers

UISegmentedControl in the Navigation Bar with the Back button

I'm adding a UISegmentedControl to the Navigation bar programatically where the titleView should be. But as Apple docs have mentioned under titleView, This property is ignored if leftBarButtonItem is not nil. But I want to have the back button as…
Isuru
  • 30,617
  • 60
  • 187
  • 303
15
votes
5 answers

How can I enable multiple segments of a UISegmentedControl to be selected?

Lets say my UISegmentedControl has 8 numbered segments. I would like for the user to be able to turn on 2, 3, or more of them at once. Toggling them. Essentially like a bits in a byte. Is this possible? I believe it is on regular Mac OS X but I…
dlamblin
  • 43,965
  • 20
  • 101
  • 140