Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically add a UISegmentedControl to a container view

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

like image 483
Alede Avatar asked Jul 14 '11 03:07

Alede


People also ask

How do you implement segment control in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Segmented Control to the top of the main view. Also drag a Label to the view and place it below the Segmented Control. Select the label and give it a text of First Segment selected.


1 Answers

this one is perfect I tested.....

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 in your class.

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

For deprecated UISegmentedControlStyle you can take a look on this URL.

like image 154
Muhammad Rizwan Avatar answered Sep 28 '22 22:09

Muhammad Rizwan