我通过以下代码将工具栏设置为纵向:

    self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];

由于默认情况下状态栏是隐藏的,因此它在横向方向上存在间隙:

ios - 旋转后固定UIToolbar位置的正确方法-LMLPHP

旋转设备时,是否每次都需要更新工具栏框架(以避免间隙),并在加载当前视图时检查方向?



还有另一种解决方法吗?

最佳答案

您可以在工具栏上添加约束,而不必设置框架:
像这样

    self.toolbar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide][toolbar(toolbarHeight)]" options:0 metrics:@{@"toolbarHeight": [NSNumber numberWithInt:44]} views:@{@"toolbar": self.toolbar}, @"topLayoutGuide":self.topLayoutGuide}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[toolbar]|" options:0 metrics:nil views:@{@"toolbar": self.toolbar}]];

关于ios - 旋转后固定UIToolbar位置的正确方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32874823/

10-13 05:09