【问题标题】:How to add vertical space between UINavigationBar and UISegmentedControl?如何在 UINavigationBar 和 UISegmentedControl 之间添加垂直空间?
【发布时间】:2014-03-03 14:45:57
【问题描述】:

我在这里完全以编程方式处理我的 UI。当我将UISegmentedControl 添加到我的UITableViewControllerheader view 时,它只是将自己固定在那里,UINavigationBar 之间没有垂直空间。如何在 UISegmentedControlUINavigationBar 之间添加一些填充?

【问题讨论】:

  • 分段控件的修改框架无效?
  • 我想知道是否可以以某种方式修改header view 属性。

标签: ios iphone objective-c uinavigationbar uisegmentedcontrol


【解决方案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;
}

当然,您可以多弄些框架来让物品按您想要的方式定位。

【讨论】: