Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UISegmentedControl to UITableView

How do I add a UISegmentedControl to a UITableView? Similar to the image found here: http://cdn.imore.com/sites/imore.com/files/styles/large/public/field/image/2012/08/iHomework-for-iPhone-managing-assignments-and-tasks.jpg

I would like to have a UISegmentedControl that sticks underneath the navigation bar.

This is what I've tried so far:

In my viewDidLoad method, I've created a UISegmentedControl

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];
segmentedControl.frame = CGRectMake(50, 0, 220, 100);
[segmentedControl addTarget:self action:@selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];

However, the segmented control overlaps with the tableview itself.

EDIT

Additionally,I would like to create this uisegmentedcontrol fully programmatically as I don't want to use a nib file.

like image 646
waylonion Avatar asked Jan 12 '23 13:01

waylonion


1 Answers

   self.tableView.tableHeaderView = segmentedControl;

If you want it to obey your width and height properly though enclose your segmentedControl in a UIView first as the tableView likes to mangle your view a bit to fit the width.

enter image description hereenter image description here

like image 131
David Wong Avatar answered Jan 18 '23 19:01

David Wong