【问题标题】:Correct way to fix UIToolbar position after rotation旋转后修复 UIToolbar 位置的正确方法
【发布时间】:2015-10-01 17:34:13
【问题描述】:

我通过此代码为纵向设置工具栏:

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

因为状态栏默认是隐藏的,所以横向有间隙:

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

还有其他方法可以解决吗?

【问题讨论】:

    标签: ios objective-c orientation uitoolbar


    【解决方案1】:

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

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

    【讨论】:

    • 在这种情况下工具栏与纵向的状态栏重叠
    • @sx00 检查我编辑的答案。那应该这样做。我没有考虑到 topLayoutGuide。
    猜你喜欢
    • 2021-10-11
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多