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
6
votes
1 answer

how to auto-stretch UISegmented Control horizontally to fill full UITableViewCell?

How to auto-stretch UISegmented Control horizontally to fill full UITableViewCell? In a manner in which it will auto-resize after an orientation change too. I have tried the following however this still doesn't work. The segmented control appears…
Greg
  • 34,042
  • 79
  • 253
  • 454
6
votes
2 answers

Automatic resizing in a navigation bar of a segmented control built dynamically

A controller adds a UISegmentedControl to a navigation bar. The segmented control is added to the navigation bar in the viewDidLoad method of the controller but the actual segments are created dynamically after viewDidLoad is called. I can't get the…
ptrico
  • 1,049
  • 7
  • 22
6
votes
4 answers

How to check whether the UISegmentedControl is selected or not by user?

I got an app in which I allocate 5 UISegmentedControl dynamically into the view. And got a Done button at the end. My condition that to proceed into next step (when done button is pressed), all the UISegmentControls "should be selected by user". The…
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
6
votes
2 answers

UISegmentedControl value changed programmatically

How can I hook up my UISegmentedControl's value changed method programmatically. I know it's possible using IB but I was wondering how to do it with code. Thanks.
sumderungHAY
  • 1,337
  • 18
  • 30
6
votes
3 answers

UISegmentedControl selected index always 0?

I have a UISegmentedControl that has 6 segments which I created in Interface Builder. I am calling the following method on value changed: -(IBAction)segmentedChartButtonChanged:(id)sender { switch (self.segmentedChartButton.selectedSegmentIndex)…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
6
votes
1 answer

segmented controls type in Android app

I try to make buttons like those on the desk of android: This button are similar to the uisegmentedcontrol for Iphone. I don't find a easy way to do this in Android. For the moment, I use the Button explain…
eclosiondroid
  • 61
  • 1
  • 3
6
votes
2 answers

SwiftUI: how to get rid of Segmented Control text movement when an alert is shown?

In a small sample SwiftUI app, I have a settings view that shows a couple of option selections, implemented as segmented controls. The text in these segmented controls visibly moves when an alert is presented or dismissed. Is there a way to get rid…
Gereon
  • 17,258
  • 4
  • 42
  • 73
6
votes
3 answers

Segmented Controller background grey after iOS13

I'm currently having problems with an iOS 13 segmented controller. I have this method in which I change the appearance of my segmented controller, it worked great until iOS 13 got out. now I the segmentedController has a grey background always, even…
Jalil
  • 1,167
  • 11
  • 34
6
votes
3 answers

Scroll dynamic UISegmentedControl in Swift 3

Hi i want to create a dynamic UISegmented View with more than 20 Items. I tried it, but the output is like this. the text is sliced not visible fully. I want it to scroll to the left and right and also display the full text. can some one help me…
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
6
votes
5 answers

UISegmentedControl in Mail app

How do I get a UISegmentedControl that is like the one in the Mail App, so that it is the same colour as UIToolbar buttons (as if both segments were in the selected state). I want to use the segmented control for exactly the same purpose as…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
6
votes
2 answers

UIToolbar with UISegmentedControl AutoLayout (/full width)

I have a UIViewController with a UIToolBar below the UINavigationBar and a UITableView below that. The only UIBarButtonItem on my UIToolBar is a UISegmentedControl as in the screenshot below: However, how do I get the…
Aleksander
  • 2,735
  • 5
  • 34
  • 57
6
votes
3 answers

detect event when tapped on already selected segment

Hi i want to get event when i touch on already selected segment. i have implemented below solution import UIKit class MARSSegmentController: UISegmentedControl { /* // Only override drawRect: if you perform custom drawing. // An…
Dattatray Deokar
  • 1,923
  • 2
  • 21
  • 31
6
votes
7 answers

How to programatically set the selected state for a segment in UISegmentedControl

I have four segments in my UISegmentedControl. I am trying to programmatically set the selected state of the third and fourth segments at the same time if the user has selected the first segment. Example: In a given segmented control, if the user…
Revanth
  • 273
  • 1
  • 5
  • 10
6
votes
5 answers

UISegmentedControl only changes text color when revisiting ViewController

UPDATE ANSWERED. BY ME. Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the…
Chisx
  • 1,976
  • 4
  • 25
  • 53
6
votes
6 answers

UISegmentedControl without rounded corner?

Is there a way of get rid of UISegmentedControl's rounded corners or it is the default behavior?
Robert Childan
  • 983
  • 1
  • 12
  • 22