我有一个自定义的 UIToolbar
,当标签栏被隐藏时我会显示它。工具栏按钮太靠近 iPhone X 上的主页指示器:
let toolbar = UIToolbar()
let height = tabBarController?.tabBar.frame.height
toolbar.frame = CGRect(x: 0, y: view.bounds.height - height, width: view.bounds.width, height: height)
toolbar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
view.addSubview(toolbar)
这就是我想要的样子(邮件应用程序)^
由于这是一个自定义 View ,我知道我可以更改 y 位置并将其移动到安全区域的底部,但我宁愿移动按钮。我使用的是普通 UIBarButtonItem
,中间有灵活的空间。
最佳答案
在 iOS 11
中,Apple 弃用了顶部和底部布局指南
,取而代之的是单个安全区域布局指南
。
因此,使用 Safe Area Layout Guides
将 View 从主页指示器移至上方。
使用 Storyboard:
- 转到
Storyboard
并在Interface Builder Document 部分
- 勾选
使用安全区域布局指南
复选框 - 然后将
Bottom Constraint
更改为相对于Safe Area
现在 View 在 Home Indicator
上方对齐。
或通过编码,
toolbar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
toolbar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
toolbar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
toolbar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
toolbar.heightAnchor.constraint(equalToConstant: 50)
])
关于ios - 自定义 UIToolbar 太靠近 iPhone X 上的主页指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236375/