如何在UINavigationBar和UISegmentedControl之间添加垂直空间?

时间:2014-03-03 14:45:58

标签: ios iphone objective-c uinavigationbar uisegmentedcontrol

我在这里完全以编程方式使用我的UI。当我向UISegmentedControl的{​​{1}} UITableViewController添加header view时,它只是将自己固定在那里,UINavigationBar之间没有垂直空格。如何在UISegmentedControlUINavigationBar

之间添加一些填充

1 个答案:

答案 0 :(得分:1)

实例化UIView对象,并将UISegmentedControl添加为子视图。然后将UIView设置为您的表格headerView。您可以通过调整您创建的UIView的框架来添加填充。

- (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;
}

当然,您可以更多地使用框架,以便按照您的需要定位。