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

Switching between 3 pages with segmentend button requires 2 taps?

Here is my setup : Home page, Profile Page, Friends page. In each page, there is the exact same segmentend button : [Home | Profile | Friends] When I tap on a segment, a segue is called and the page changed. So, when i'm in Profile, Profile is…
Gil Sand
  • 5,333
  • 3
  • 29
  • 66
0
votes
1 answer

How to save list and show the same data when user switch back?

I am using segment controller in my application. if i click on buttons , it will fetch the data from web service and display the data in custom list view. If i go from button1 >button2 then again come back to button1, instead of displaying same list…
Biswatma
  • 109
  • 3
  • 16
0
votes
1 answer

Fixed horizontal position of Segmented Control

I have one UIImageView (building's floor map) and UIScrollView with gesture recognisers which allow me to scroll horizontally and vertically, zoom in/out. Everything works OK, but the building has two floors, so user should have the option to switch…
Richard Topchii
  • 4,569
  • 3
  • 27
  • 70
0
votes
3 answers

Programmatically add a UISegmentedControl to a UINavigationBar

How do I PROGRAMMATICALLY add a UISegmentedControl to a UINavigationBar? I do not want to use a XIB file for this. I have a UIView with a UITableView that is added as a subview. I have tried two methods but both are not satisfactory: 1)…
wayway
  • 6,439
  • 5
  • 44
  • 84
0
votes
0 answers

Segmented Control with two segment

I have a method that creates a segmented control (UISegmentedControl class) with only two segment "ON" and "OFF". The problem is that I want to initialize the segmented control on the first segment (ON) but any int that I set for the method…
perascotta
  • 133
  • 9
0
votes
2 answers

How to update UISegmentedControl

I'm buidling an app in which I want to allow my user to filter an array of data (employees) based on one criteria (company they work in). I want to do this using UISegmentedControl. I also want to allow my user to edit the array of companies ad any…
Joris416
  • 4,410
  • 4
  • 26
  • 47
0
votes
2 answers

UISegmentedControl Text Color Issue

I am using this for changing the text color of text of segmented control but its color is not getting changed in UIControlStateNormal. I have to kept the same color for both states. NSDictionary *highlightedAttributes = [NSDictionary …
Ankur
  • 98
  • 2
  • 13
0
votes
0 answers

UiSegementedController's segment automatically select in UITableViewCell when scroll the uitableview

I have added UISegmentedController to TableViewCell in CellForRowAtIndexPath. Following is the code. [cell.segStatus addTarget:self action:@selector(selectedSegmentControl:) forControlEvents: UIControlEventValueChanged]; [cell.segStatus…
0
votes
1 answer

When toggling using UISegmentedControl between two layouts very fast I get a thread 1 exc_bad_access code=2 error

I've noticed I can create a crash in my app by quickly switching between two UICollectionViewFlowLayout's by tapping a segment of my UISegmentedControl. The crash error I get is in this email: In this method: - (void)displayTypeSegmentSelected { …
LondonGuy
  • 9,643
  • 9
  • 68
  • 143
0
votes
2 answers

Change color of outer border for SegmentedControl in Objective-C

I have a Segmented Control object, and I want to change the color of the outer border while preserving the line between the two options. I currently have the code: self.segmentedControl.tintColor = [UIColor whiteColor]; //sets border and text to…
rkade93
  • 3
  • 4
0
votes
1 answer

UIImage's resizableImageWithCapInsets places a shadow on image

I'm having trouble customising an UISegmentedControl: I've subclassed it I'm setting it's background for both the selected state and the unselected state like this: #define kEdgeInsets UIEdgeInsetsMake(18, 18, 18, 18) UIImage *grayImage =…
Pierluigi Cifani
  • 224
  • 2
  • 13
0
votes
1 answer

Remove white squares around text in UISegmentedControl

Under IOS7, I've defined a UISegmentedControl in the interface builder and use the following code to customize it: segconOptions.layer.cornerRadius = 20.0; segconOptions.layer.masksToBounds = YES; //[segconOptions setTintColor:[UIColor…
Thunk
  • 3,829
  • 7
  • 24
  • 43
0
votes
1 answer

UINavigationBar Button Not Disabling and Enabling in the right Logic

I have a simple application where the user inputs information into a series of UITextFields and a few other components in order for the app to populate with information. I have 4 UITextFields (Name, Event, Subevent and Amount), 1 UITextView…
amitsbajaj
  • 1,244
  • 1
  • 21
  • 57
0
votes
2 answers

How Do I add multiple table views to a view controller

As the question asks I would like to know how to make multiple table view controllers on the same view controller. SOrry about the formatting but in the end I added a segmented control change function that will set which table views are hidden and…
user2446334
  • 143
  • 1
  • 3
  • 6
0
votes
1 answer

Set different text colour for each UISegementedControl index

Is it possible to have each segment in a UISegmentedControl's text / font colour to be different? I see to be able to only set the global colour and this is not what I am looking for.
Robert J. Clegg
  • 6,549
  • 8
  • 43
  • 92
1 2 3
99
100