【问题标题】:iOS 11 UIToolBar automatic expansioniOS 11 UIToolBar 自动扩展
【发布时间】:2017-09-13 18:10:02
【问题描述】:

我正在以编程方式创建 UIToolBar,它工作正常,除了 iPhone X,我希望工具栏根据手机的方向展开。如果工具栏是使用界面生成器创建的,则扩展工作。

我没有设置任何高度限制,即使是通过自动布局掩码隐式设置。

代码:

UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:toolbar];
[self.view addConstraints:@[
                            [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
                            [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
                            [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeRight multiplier:1 constant:0]]];

那么,我应该怎么做才能使工具栏正常运行?

【问题讨论】:

    标签: uitoolbar ios11


    【解决方案1】:

    好的,部分答案是使用安全区域self.view.safeAreaLayoutGuide,或者使用上下文:

    [NSLayoutConstraint constraintWithItem:self.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
    

    “部分”部分是指IB工具栏的高度为49,手动工具栏的高度为44。

    这对我来说已经足够好了,但如果有人能够修复/解释 44-49 的差异,我会将其标记为解决方案。

    编辑:我找到了缺失的部分。

    我没有打电话给invalidateIntrinsicContentSize。添加来自willTransitionToTraitCollection: withTransitionCoordinator: 的呼叫修复了该问题。不知何故,在去年左右,我错过了工具栏在紧凑模式下改变其大小。

    【讨论】:

    • 我相信底部工具栏的新高度差是为了解决屏幕底部的“抓取栏”,以引导用户“向上滑动回家”的手势。
    • @BenKreeger 是的,这由我写的内容处理。我不知道如何处理这样一个事实,即使内在大小是 49,约束有时也会保持在 44。而且 XIB 似乎不会发生这种情况。
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2013-07-28
    相关资源
    最近更新 更多