uitoolbar - iOS 11 UIToolBar自动扩展

标签 uitoolbar ios11

我正在以编程方式创建一个 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/

相关文章:

iOS UIToolbar 识别触摸

iphone - 尝试使用隐藏的 UINavigationBar 实现持久 UIToolBars 似乎是在操作未记录的 View 类

ios - setItems 中的空白工具栏

iOS11蓝牙有一些奇怪的东西

swift - PDFAnnotationSubType 持久时不保存墨水(iOS11、PDFKit)

iOS11 文件提供程序扩展不起作用

iphone - 在 iPad 的 DetailViewController ToolBar 上添加工具栏项或按钮

ios7 - 显示/隐藏 UIToolbar, "match finger movement",例如 iOS7 Safari

选择取消按钮错误后的 iOS 11 UIImagePickerController

jquery - 设置隐藏的下拉选择值会导致页面在 iOS 11 safari 中重新加载