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
7
votes
4 answers

UISegmentedControl maintain aspect ratio on image

I'm having a hard time making images on a UISegmentedControl keep their aspect ratio and not scale to all sides. Here's what it looks like: The images are 100X100(except nr 6 gonna replace soon) and I want them to maintain the aspect ratio. I have…
JoakimE
  • 1,820
  • 1
  • 21
  • 30
7
votes
1 answer

Selected state of text in a segmented control in Xcode

I am working on my first application using Swift and Xcode. In one of my views, I have a segmented control which has emoticons as the text being displayed. However, I have a problem where whenever one of the emoticons is selected, it becomes blacked…
user5739562
  • 190
  • 1
  • 8
7
votes
2 answers

how to remove border from UISegmentController?

I want to remove border of UISegmentController. If it is possible. otherwise change it in the custom border color.
Abdul Hameed
  • 1,025
  • 13
  • 17
7
votes
1 answer

UISegmented Contol set to Momentary Handler

This is probably a really simple question but I can't seem to find anything in the APIs or across any search engine. I have a Segmented control that i have set to momentary as a user will select a couple of makes of a car that they want to search…
Adam
  • 152
  • 8
7
votes
10 answers

How to set UISegmentedControl Tint Color for individual segment

Starting to learn Swift and am attempting to convert this ObjectiveC code: [[mySegmentedControl.subviews objectAtIndex:0] setTintColor:[UIColor blueColor]] This correctly sets the tint color of the first segment. This is the closest I've come to…
Automate This
  • 30,726
  • 11
  • 60
  • 82
7
votes
3 answers

Two lines of text in a UISegmentedControl

Try as I might, I can't solve a UISegmentedControl bug for an iOS7 iPhone app. When I create the segmented control, I use this code: NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm",…
herzian
  • 231
  • 2
  • 8
7
votes
3 answers

UISegmentedControl get previous selected index

I have a UISegmentedControl with a segmentedValueChanged method on it. I need to find out what was the selected index before this method is called . How to do this?
user1028028
  • 6,323
  • 9
  • 34
  • 59
7
votes
3 answers

UISegmentedControl setTitleTextAttributes does not work

So I tried to change the text attribute of the title of my UISegmentedControl, but it doesn't work, nothing change. I have also applied a custom background and divider and it works correctly, but not this. NSDictionary *normaltextAttr = …
harinsa
  • 3,176
  • 5
  • 33
  • 53
7
votes
2 answers

How can a UIView get a callback when it is added to its superview?

Problem I have subclassed a UIView. After an object of this subclass is added to its superview, it needs to autonomously run some code. How can I hook on to this event to run my code? Why I Need It The background of the selected segmented of a…
Pwner
  • 3,714
  • 6
  • 41
  • 67
7
votes
6 answers

How to tint the background images of an UIButton programmatically and dynamically?

I am working on an app - or rather on some re-usable "framework" which I am happy to share once it works. Within this app the user should be able to choose from a list of color themes. Therefore the app must be able to tint its UI elements in some…
Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
7
votes
1 answer

Setting images for UISegmentedControl for different states?

I have a UISegmentedControl in my XIB that has four segments. My designer and myself have decided to have certain images for default-state, and other images for the selected-state. Each segment will have different images, for BOTH states. It seems…
Redneys
  • 285
  • 1
  • 6
  • 18
7
votes
2 answers

iOS - Prevent UIAppearance settings from changing UIWebView input accessory view

I have a UIWebView in my app, and the problem is that I have a UIAppearance that modifies the appearance of segmented controls, so it modifies the segmented control in the input accessory view for UIWebView textfields, I'd like for it to look…
8vius
  • 5,786
  • 14
  • 74
  • 136
7
votes
2 answers

How can I have a space-filling segmented control in the toolbar in an iOS app?

I have a segmented control. Whenever the view has finished appearing I create a bar button item to hold it and set that as the toolbars item. The problem I'm having is that the segmented control will not fill up the space in the toolbar, even though…
6
votes
6 answers

Get frame from certain UISegmentedControl index?

In my app, I use a UIPopoverController and I use the presentPopoverFromRect API. What I am doing now is just setting it to the frame of my whole UISegmentedControl. However I want to be more precise than this. Is there any way to get the frame of a…
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
6
votes
1 answer

How to dismiss UIActionSheet with close button and avoid crash?

I have read on another post (how to dismiss action sheet) that I can use [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; to dismiss the uiactionsheet with the close button, as defined in: UIActionSheet *actionSheet = [[UIActionSheet…