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.

1793 questions
321
votes
17 answers

Color Tint UIButton Image

I noticed that when I place a white or black UIImage into a UISegmentedControl it automatically color masks it to match the tint of the segmented control. I thought this was really cool, and was wondering if I could do this elsewhere as well. For…
Logan Shire
  • 4,555
  • 4
  • 23
  • 32
225
votes
16 answers

Change font size of UISegmentedControl

Can anyone please tell me how can I change the font type and size of UISegmentedControl?
Aashutosh Tiwari
  • 2,271
  • 2
  • 15
  • 5
120
votes
15 answers

How to change the colors of a segment in a UISegmentedControl in iOS 13?

A UISegmentedControl has a new appearance in iOS 13 and existing code to alter the colors of the segmented control no longer work as they did. Prior to iOS 13 you could set the tintColor and that would be used for the border around the segmented…
rmaddy
  • 298,130
  • 40
  • 468
  • 517
98
votes
10 answers

UISegmentedControl below UINavigationbar in iOS 7

How do I make a UISegmentedControl as a part of an UINavigationBar below it? Is it connected to the UINavigationBar or is it a complete separate view just added as a subview to the UINavigationController's view controller. Looks like it is part of…
yoeriboven
  • 3,393
  • 2
  • 22
  • 40
87
votes
4 answers

get string value from UISegmentedControl

How can I get the text value of a segment in a UISegmentedControl?
Matt S.
  • 12,487
  • 13
  • 71
  • 125
83
votes
12 answers

How to change font color of UISegmentedControl

I try to change font color from white to black for UISegmentedControl (for iOS 4.*) UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease]; button.momentary =…
Oksana
  • 12,112
  • 8
  • 49
  • 88
82
votes
9 answers

How do I use a UISegmentedControl to switch views?

I'm trying to figure out how to use the different states of a UISegmentedControl to switch views, similar to how Apple does it in the App Store when switiching between 'Top Paid' and 'Top Free'.
Mark Adams
  • 30,342
  • 11
  • 73
  • 77
78
votes
7 answers

UISegmentedControl change number of segments programmatically

Is there a way to change the number of segments programmatically?
Beppino66
  • 1,539
  • 2
  • 12
  • 15
75
votes
7 answers

How do I switch UIsegmentedControl programmatically?

How do I switch UISegmentedControl programmatically ?
Voloda2
  • 11,699
  • 18
  • 77
  • 126
70
votes
23 answers

UISegmentedControl selected segment color

Is there any way to customize color of selected segment in UISegmentedControl? I've found segmentedController.tintColor property, which lets me customize color of the whole segmented control. The problem is, when I select bright color for tintColor…
Mike
  • 1,622
  • 3
  • 17
  • 17
67
votes
16 answers

iOS: change the height of UISegmentedcontrol

I am trying to change the height of UISegmentedcontrol, but it is not allowed in the Interface Builder. Is there any way to change or it is impossible? Thanks
DavidNg
  • 2,666
  • 5
  • 28
  • 45
66
votes
7 answers

How to programmatically add a UISegmentedControl to a container view

How would I define the frame for a UISegmentedControl? I would like the segmented control to appear at the bottom of a container view i.e UIView.
Alede
  • 661
  • 1
  • 5
  • 4
65
votes
8 answers

Define click event for UISegmentedControl

I have added a UISegmentedControl in my application. None of the buttons are selected in normal state. I want to implement a button click event when the first segment is selected, and another event when another button is clicked.
Warrior
  • 37,935
  • 44
  • 133
  • 211
64
votes
20 answers

UISegmentedControl register taps on selected segment

I have a segmented control where the user can select how to order a list. Works fine. However, I would like that when an already selected segment is tapped, the order gets inverted. I have all the code in place, but I don't know how to register the…
Pieter Jongsma
  • 3,285
  • 3
  • 23
  • 29
56
votes
2 answers

How to fix Error: this class is not key value coding-compliant for the key tableView.'

I made an app with Table View and Segmented Control, and this is my first time. I'm using some code and some tutorials, but It's not working. When I run my app It's crashing and it's showing this Error in logs: MyApplication[4928:336085] *…
Emm
  • 1,402
  • 2
  • 15
  • 41
1
2 3
99 100