Вопрос

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.

Это было полезно?

Решение

   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 here enter image description here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top