How to programmatically add a UISegmentedControl to a container view

How to define a frame for a UISegmentedControl ? I would like the segmented control to appear at the bottom of the container view ie UIView .

+50
objective-c iphone swift uisegmentedcontrol
Jul 14 2018-11-11T00:
source share
7 answers

I tested this perfect .....

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)]; scroll.contentSize = CGSizeMake(320, 700); scroll.showsHorizontalScrollIndicator = YES; NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentedControl.frame = CGRectMake(35, 200, 250, 50); segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged]; segmentedControl.selectedSegmentIndex = 1; [scroll addSubview:segmentedControl]; [segmentedControl release]; [self.view addSubview:scroll]; 

Then add your method to your class.

 - (void)MySegmentControlAction:(UISegmentedControl *)segment { if(segment.selectedSegmentIndex == 0) { // code for the first button } } 

For the deprecated UISegmentedControlStyle, you can see this URL.

+176
Jul 14 2018-11-11T00:
source share
โ€” -

U can do like this:

 UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]]; [segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar]; segmentControl.frame = CGRectMake(10, 50, 300, 30); [segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged]; [segmentControl setSelectedSegmentIndex:0]; [scrollView addSubview:segmentControl]; [segmentControl release]; 

Step 2:

 -(void)segmentedControlValueDidChange:(UISegmentedControl *)segment { switch (segment.selectedSegmentIndex) { case 0:{ //action for the first button (Current) break;} case 1:{ //action for the first button (Current) break;} } } 
+17
Nov 16 '13 at 5:52
source share

Step 1. Create index segment management

 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common-bg.jpg"]]; [self.navigationItem setHidesBackButton:YES]; //-- For creating segment control in navigation bar UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Month", @"Year", @"Home", nil]]; [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar]; mainSegment.frame = CGRectMake(0,0, 400, 43); self.navigationItem.titleView = mainSegment; mainSegment.selectedSegmentIndex = 1; [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged]; [self.view addSubview:mainSegment]; //--**-- } 

Step 2. Create a subview

 - (void)mainSegmentControl:(UISegmentedControl *)segment { if(segment.selectedSegmentIndex == 0) { // action for the first button (Current or Default) } else if(segment.selectedSegmentIndex == 1) { // action for the second button } else if(segment.selectedSegmentIndex == 2) { // action for the third button } else if(segment.selectedSegmentIndex == 3) { // action for the fourth button } } 
+7
Nov 08 '13 at 9:40
source share

This is similar to the current answers, but cleared up a bit for iOS 8 devices.

 NSArray *itemArray = [NSArray arrayWithObjects: @"Uno", @"Dos", @"Tres", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentedControl.frame = CGRectMake(35, 200, 250, 50); [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged]; segmentedControl.selectedSegmentIndex = 1; [self.view addSubview:segmentedControl]; 
  • Create an array to store values โ€‹โ€‹for the segment
  • Initialize a segment using an array
  • Assign it the location on the screen and the size of the control
  • Point it to a method that is called when the user interacts with it.
  • Choose the default value (in this case, Dos)
  • Put it on the main view

Then create a segmentAction method that is called when the user changes the value

 - (void)segmentAction:(UISegmentedControl *)segment { switch (segment.selectedSegmentIndex) { case 0: // Uno break; case 1: // Dos break; case 2: // Tres break; default: break; } } 

I just prefer the switch statement because it is cleaner to watch. You can improve it by creating an enumeration and using the values โ€‹โ€‹in it for parameters (optionUno, optionDos, optionTres) instead of 0,1,2.

+6
Nov 26 '14 at 18:17
source share

To add the UISegmentedControl code to the container, follow these steps:

 // Create UISegmentedControl object to add control UISegment. UISegmentedControl *objSegment = [[UISegmentedControl alloc] initWithItems:array]; // Set frame for objSegment Control (formate: (x, y, width, height)). where, y = (height of view - height of control). [objSegment setFrame:CGRectMake(0, (self.view.frame.size.height - 40), 320, 40)]; // handle UISegmentedControl action. [objSegment addTarget:self action:@selector(handleSegmentControl:) forControlEvents: UIControlEventValueChanged]; // Add your UISegmentedControl in your view. [self.view addSubview:objSegment]; 

If you have any questions, please contact me.

+5
Nov 07 '15 at 6:26
source share

Swift:

 let items = ["All Fruits", "Orange", "Grapes", "Banana"] let filtersSegment = UISegmentedControl(items: items) filtersSegment.frame = CGRect.init(x: 0, y: 0, width: 300, height: 50) filtersSegment.selectedSegmentIndex = 0 filtersSegment.tintColor = UIColor.black filtersSegment.addTarget(self, action: #selector(self.filterApply), for: UIControlEvents.valueChanged) self.view.addSubview(filterSegment) @objc private func filterApply(segment: UISegmentedControl) -> Void { switch segment.selectedSegmentIndex { case 1: //Do something for Orange case 2: //Do something for Grapes case 3: //Do something for Banana default: //Do something for All Fruits } } 
+1
May 29 '17 at 6:18
source share

This will work for all types of iOS devices:

 UISegment *segment = [[UISegmentedControl alloc] initWithItems:array]; segment.frame = CGRectMake(0, self.view.frame.size.height - 40, 300, 40); UIFont *font = [UIFont fontWithName:@"DroidSans" size:18.0f]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; [segment setTitleTextAttributes:attributes forState:UIControlStateNormal]; [segment addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged]; [self.view addSubview:segment]; 
0
Aug 17 '15 at 5:37
source share



All Articles