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

标签 ios iphone objective-c uinavigationbar uisegmentedcontrol

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

最佳答案

实例化一个 UIView 对象并将您的 UISegmentedControl 添加为 subview 。然后将 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;
}

当然,您可以对框架进行更多调整,以使事物按您想要的方式定位。

关于ios - 如何在 UINavigationBar 和 UISegmentedControl 之间添加垂直空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149686/

相关文章:

iphone - iOS UIDatePicker时区问题

iphone - 尝试运行应用程序时,xcode模拟器使xcode崩溃

ios - 核心数据和 GCD : Passing the correct managed object context to custom NSManagedObjects

iOS App Archive 验证因错误而失败

ios - 如何在 swift 中创建负的 Firebase 时间戳

ios - 获取空的用户名字符串

iphone - 找不到 'xxxxx' 的协议(protocol)声明;你是说 'yyyyy'

ios - 在 iOS 中正确使用 View Controller 和 MVC 模式

ios - Storyboard和 UITabBarController

苹果手机 : How to implement the page flip effect without curl?