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

UISegmentedControl image highlighting bug in iOS6

Currently I have a small segmented control with 3 separate segments. What I want to do is, if selected, change the image of that specific segment to a different image. So far I've managed to make it very similar to what I want, and a new image is…
dsgriffin
  • 66,495
  • 17
  • 137
  • 137
10
votes
2 answers

setting background image of UISegmentedControl

So I have the following code to set the segmented control background color: UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)]; [segmentedCtrl setBackgroundImage:[UIImage…
adit
  • 32,574
  • 72
  • 229
  • 373
9
votes
1 answer

How to change UISegmentcontrol font and selected segment colour?

Possible Duplicate: UISegmentedControl selected segment color UISegmentcontrol appearances causing issues Hi i will like to change the default UISegmentControl font to a custom font and change the selected segment color to another color instead…
Desmond
  • 5,001
  • 14
  • 56
  • 115
9
votes
3 answers

UISegmentedControl image scaling

How can I scale down the images used in a UISegmentedControl? I am creating the segmented control programmatically: UISegmentedControl * segmentButton; segmentButton = [UISegmentedControl segmentedControlWithItems: [NSArray…
e.James
  • 116,942
  • 41
  • 177
  • 214
9
votes
5 answers

How to resize text (font) to fit in UISegment of UISegmentedControl?

Is there any way to reduce font size that can be fit in single segment of UISegmentedControl ? Have tried many thing something like, [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth]; [[UILabel…
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
9
votes
9 answers

Swift: Background color change in selected segment of UISegmentControl

In my Swift app, I dont know how to change BGColor for selected segment alone, in UISegmentControl. I have tried a lot, only tint color is changing. I got success in Objective - C. I dont know how to convert that to SWIFT. Kindly guide me. My…
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
9
votes
2 answers

Why are my UISegmentControl segments highlighting in iOS 8 when I have set background images for all states?

In iOS 6/7, I have used UISegmentedControl with background images to create an effect like so: I accomplished this by setting the background image for the UISegmentedControl for each of standard states, like so: UIImage…
Acey
  • 8,048
  • 4
  • 30
  • 46
9
votes
7 answers

iOS - UISegmentedControl Strange Behaviour

I have added a UISegmentedControl in the application. The segment control works fine and all i am doing with it is to get its selected state when a value is changes nothing else. The UISegmentedControl initially looks like this - After i show an…
user2043155
9
votes
3 answers

Text Alignment issue when I set the custom font

When I set the custom font for the segmented control then it changes the vertical text alignment. I am using below code to set the font . // I dont think these lines are creating any issue but just wanted to paste all the code …
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
9
votes
2 answers

How to get value from segmented control to know which one was picked

I want to be able to use a segmented control, but I don't know how to get the value for which one was picked. I want to know so I can use a boolean value to determine between the two that were picked
user3000847
  • 191
  • 1
  • 2
  • 11
9
votes
2 answers

Array filter vs. Core Data pull performance

I'm wondering about the performance differences between two different methods of data filtering. Here's what I'm working with: A set of core data objects A UISegmentedControl that represents a boolean filter (learned vs. not learned) A UITableView…
Whoa
  • 1,334
  • 11
  • 26
9
votes
1 answer

UISegmentedControl Color in a UIToolbar

My question revolves around the distinction of a UISegmentedController on a UINavigationBar vs a UIToolbar. If I drop a UISegmentedControl into a navigation bar as follows: navigationBar.barStyle = UIBarStyleBlackTranslucent; all is well. The…
Luther Baker
  • 7,236
  • 13
  • 46
  • 64
9
votes
8 answers

UISegmentedControl with Image and Title

I am using a UISegmentedControl inside a UIToolBar as button. I can't use a normal UIButtonBarItem, because I need to set the tintColor and support iOS 4.3. So I'm using following code: UISegmentedControl* searchButton = [[UISegmentedControl alloc]…
D-32
  • 3,245
  • 2
  • 22
  • 33
8
votes
2 answers

How can I disable a UISegmentedControl?

I have the following UISegmentedControl, which I want to be disabled: -(void)displayCheckMark { titleSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; [titleSegmentedControl insertSegmentWithImage:[UIImage…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
8
votes
0 answers

How to set the ios13 UISegmentedControl backgroundColor to white?

I am trying to set the background color of a segmentedControl in iOS 13 for the normal state. I have the same issue as listed here: How to set backgroundColor of UISegmentedControl to white in iOS 13 The answer to the question in the link above is…
ll_cool_aid
  • 109
  • 8