我在这里完全以编程方式使用我的UI。当我向UISegmentedControl
的{{1}} UITableViewController
添加header view
时,它只是将自己固定在那里,UINavigationBar
之间没有垂直空格。如何在UISegmentedControl
和UINavigationBar
?
答案 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;
}
当然,您可以更多地使用框架,以便按照您的需要定位。