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
13
votes
3 answers

How can I disable horizontal scrolling and page will change on click in paging using UIScrollView?

I have a UIScrollView with pagingEnabled. Added 5 UIStackView to subview of UIScrollView and also had one UISegmentedControl. I want to show currently selected segment and also want to disable horizontal scrolling but the view will able to scroll…
13
votes
5 answers

How to detect change in UISegmentedControl from a separate IBAction

I have a UISegmentedControl button with three segments. In ViewController.m this is working just fine -- pressing the buttons fires the correct methods. I have another separate UIButton that when it is pressed it needs to first CHECK the state of…
mstace
  • 177
  • 1
  • 1
  • 8
12
votes
5 answers

Is it possible to create multi line UISegmentedControl?

I have a relative longer text items in my segmented control so I need to break text at certain points. Is it possible to use line breaks? I know at buttons I need to set line break to word wrap, but how to to set it for UISegmentedControl.
János
  • 32,867
  • 38
  • 193
  • 353
12
votes
4 answers

Getting access to a UISegementedControl in Swift

I have been playing with the UISegmentedControl and want to manage it as needed. I've created it on the storyboard, hooked up the outlet and the action. It visualizes fine and the callback on the action indexchanged works. I want to do some…
Kokanee
  • 975
  • 4
  • 9
  • 20
12
votes
4 answers

Stretched images in UISegmentedControl

I'm trying to add images to my UISegmentedControl but the image is always automatically stretched to fill the entire control (see picture). I'm currently setting the image by calling setImage:forSegmentAtIndex:. How can set the image so that it…
shoopdelang
  • 985
  • 2
  • 9
  • 20
11
votes
4 answers

How can I calculate correct widths for UISegmentedControl segments?

I'm trying to use a UISegmentedControl but having trouble calculating the width of the segments. The control makes the segments all the same width which doesn't work for some titles, like this…
progrmr
  • 75,956
  • 16
  • 112
  • 147
11
votes
2 answers

how can I change the style for UISegmentedControl when it is selected by the user in Swift?

This is my UISegmentedControl written in Swift: I created it with the following code: let selectedAttributes: NSDictionary = [ NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: fontForSegmentedControl! ] …
user3766930
  • 5,629
  • 10
  • 51
  • 104
11
votes
4 answers

Scope Bar for UITableView like App Store?

Does anyone know how to add a scope bar to a UITableView? The App Store app does this sometimes, like in the picture below. I would like to use this scope bar to add sorting options for the elements in the UITableView. This would be more convenient…
Aloha Silver
  • 1,394
  • 17
  • 35
11
votes
1 answer

Rogue Animation of UISegmentedControl under Partial Curl

I have a view controller with a button that activates a Storyboard Modal Segue with the Partial Curl transition. On the view controller that is revealed, I have a UISegmentedControl. Lastly I have reduced the font size of the…
Ben Robinson
  • 1,602
  • 3
  • 16
  • 30
10
votes
3 answers

iOS 13 Segmented Control: Remove swipe gesture to select segment

TLDR: How to remove the swipe/pan gesture recognizer for UISegmentedControl on iOS 13? Hi, on iOS 13, lots changed with UISegmentedControl. Mostly, the changes were appearance-based. But there is another functionality change that is messing up my…
ellek
  • 165
  • 2
  • 9
10
votes
3 answers

UISegmentedControl setSelectedSegmentIndex: without valueChanged Action

I'm setting the selectedSegmentIndex of an UISegmentedControl via code. Whenever I do this the valueChanged action gets called. This sounds logical for me, but is there a way to set the selected segment without invoking the action? It should just…
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
10
votes
3 answers

Using a UISegmentedControl in the footer of UIPopoverController

In my iPad app Viewfinder (iTunes Link), I'm trying to recreate the look of a UISegmentedControl as seen in the footer of Keynote's Build In popover: The iPad HIG suggests using a bottom-aligned UIToolbar, but the appearance is incorrect. This…
Fraser Speirs
  • 4,642
  • 3
  • 21
  • 15
10
votes
4 answers

How to Set tint color of text only of selected segment using Swift?

I have used following code to set background of UISegmentedControl. homeSegment.setDividerImage(UIImage(named: "seperator.png"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics:…
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
10
votes
2 answers

UISegmentedControl: how to detect click on current segment?

is there a way to detect second click on a segment in UISegmentedControl? I found: Detect second click on a segment however, it is stated that: If you set a segmented control to have a momentary style, a segment doesn’t show itself as selected…
Heuristic
  • 5,087
  • 9
  • 54
  • 94
10
votes
2 answers

UISegmentedControl tint color isn't drawn correctly on iOS 7

I want to change the color of a UISegmentedControl on iOS 7. If I change the tint color in the Interface Builder, parts of it are drawn in the tint color, and parts are drawn in the standard blue. The image ought to make this clearer. I have…
Ian Howson
  • 653
  • 7
  • 14