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
8
votes
2 answers

UISegmentedControl iOS 13 clear color

On iOS 12, to get a UISegmentedControl with clear border, clear divider line, everything clear was easy. All I did was this: settingControl.tintColor = .clear let font = myFont let boldfont = myBoldFont …
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
8
votes
1 answer

UISegmentedControl embedded in a UINavigationBar/Item

I would like to embed a UISegmentedControl somewhere in my UINavigationControllers topbar. It is no problem embedding it in a UIBarButtonItem and setting it as the left or right barButtonItem. I can understand this approach when dealing with the…
RickiG
  • 11,380
  • 13
  • 81
  • 120
8
votes
6 answers

How to change selected segment tintColor of UISegmentedControl in Swift

I want to change the tintColor of UISegmentedControl selected segment in Swift 3. I've searched a lot of answers in Objective-c... This is my code: class ViewController:UIViewController{ var segment:UISegmentedControl override func viewDidLoad()…
user6631218
8
votes
3 answers

UISegmentedControl custom background image

I have UINavigationBar setup as image - some wood texture. I want to insert UISegmentedControl with 4 buttons on that bar. Buttons should have same texture with slightly changed tint. One solution would be to change tint alpha of buttons…
kviksilver
  • 3,824
  • 1
  • 26
  • 27
8
votes
6 answers

iphone ios7 segmented UISegmentedControl change only border color

Been looking around and trying to change just the border color (with a different text color) with no luck. Can change the tint, but changes both the text and border.
ort11
  • 3,359
  • 4
  • 36
  • 69
8
votes
2 answers

Adding segment controller on navigation bar in the second row

I want to add a segment controller to the navigation bar not as the title, but like this: How can I do that?
Eli_Rozen
  • 1,301
  • 4
  • 20
  • 31
8
votes
4 answers

How to add segmented control to uitoolbar

I have a UIToolbar with UIBarButtonItems on it. I want to add segmented control to it. //Add UIToolbar toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 35)]; toolbar.barStyle = UIBarStyleBlackOpaque; [self.view…
user1120133
  • 3,244
  • 3
  • 48
  • 90
8
votes
1 answer

Multiple selection with UISegmentedControl - what is Pages using?

I want to make a Bold/Underline/Italic toolbar like you see on Pages (iPhone/iPad). They use what appears to be a UISegmentedControl. But they can have both Bold/Underline selected at the same time, which doesn't seem to be possible with the current…
Luke
  • 13,678
  • 7
  • 45
  • 79
7
votes
6 answers

UITableViewCell / UISegmentedControl border issue

I'm trying to get a UISegmentedControl in a group UITableViewCell much like in the wifi settings in the Setting Application. The problem I'm having is I'm getting a double border. I get one border for the UISegmentedControl and one for the…
Daniel Wood
  • 4,487
  • 3
  • 38
  • 36
7
votes
3 answers

Detecting the tapped segment of a UISegmentedControl

I have placed an UISegmentedControl into my XIB file. Basically, when the the second tab of the control is tapped (aka segment 1, the first segment is segment 0), I want to unhide a text field. I know how to unhide the text field, but how do I…
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
7
votes
1 answer

UISegmentedControl Within UIToolBar

I know how to add a UISegmentedControl to a UIToolBar from within IB, but I am trying to do the same programmatically, because I am using a custom subclass of UISegmentedControl with doesn't have an XIB. This is the code for the…
user594161
7
votes
3 answers

Specifying UISegmentedControlNoSegment to UISegmentedControl's selectedSegmentIndex has no Effect on iOS 13

I am the maintainer of STAControls which subclasses various UIControls, one of which is UISegmentedControl. Within that project's sample app, I have the following code (can be downloaded from the aforementioned link): -…
Stunner
  • 12,025
  • 12
  • 86
  • 145
7
votes
2 answers

Transform specific segment in segmented control

Is there a way to transform the content of a specific segment contained in segmented control? (I have to rotate it by 180deg) Not the image contained in it, because in my case there isn't one (I have used FontAwesome) Demonstration: from: to:
Dorad
  • 3,413
  • 2
  • 44
  • 71
7
votes
2 answers

How to Implement Segment Control in android studio?

I need to use segmented control in Android. Is there a default control for the same? What might be the best and efficient way to do so?
Satish Kumar
  • 131
  • 2
  • 10
7
votes
3 answers

UISegmentedControl with badge number

Just like the tabbar, I want to show badge on UISegmentedControl. As I cant see any predefined methods for UISegmentedControl just like available for UITabBar. I thought about adding the badge as an image just on top of it, but maybe there is some…
Tariq
  • 9,861
  • 12
  • 62
  • 103