我正在以编程方式创建一个 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]]];
那么,我应该怎样做才能使工具栏正常工作?
最佳答案
好吧,部分答案是使用安全区域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:
的调用修复了这个问题。不知何故,在去年左右的时间里,我错过了工具栏在紧凑模式下改变其大小的情况。
关于uitoolbar - iOS 11 UIToolBar自动扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204232/