Reputation: 319
I'm working with my UI completely programmatically here. When I add a UISegmentedControl
to my UITableViewController
's header view
, it just fixes itself up there with no vertical space between the UINavigationBar
. How can I add some padding between the UISegmentedControl
and the UINavigationBar
?
Upvotes: 1
Views: 271
Reputation: 9426
Instantiate a UIView
object and add your UISegmentedControl
as a subview. Then set the UIView
as your table's headerView
. You'll be able to add padding by adjusting the frame of the UIView
you created.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150 /* <-- adjust this value for more or less padding */)];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:@[@"One", @"Two", @"Three"]];
segControl.frame = CGRectMake(0, 90, 200, 29);
//calculate the middle of the header view
CGFloat middleOfView = headerView.bounds.size.width / 2;
CGFloat middleOfSegControl = segControl.bounds.size.width / 2;
CGFloat middle = middleOfView - middleOfSegControl;
//position the seg control in the middle
CGRect frame = segControl.frame;
frame.origin.x = middle;
segControl.frame = frame;
[headerView addSubview:segControl];
self.theTableView.tableHeaderView = headerView;
}
Of course you can mess with the frames some more to get things positioned like you want them.
Upvotes: 1