自定义UIToolbar太靠近iPhone X上的主页指示器 [英] Custom UIToolbar too close to the home indicator on iPhone X
问题描述
我有一个自定义的 UIToolbar
,该标签在隐藏标签栏时显示.工具栏按钮太靠近iPhone X上的主页指示器:
I have a custom UIToolbar
that I'm showing when the tab bar is hidden. The toolbar buttons are too close to the home indicator on 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)
按钮离原位指示器太近
Buttons are too close to the home indicator
这就是我想要的样子(邮件应用)^
This is what I want it to look like (Mail app) ^
由于这是一个自定义视图,因此我知道可以更改y位置并将其移动到安全区域的底部,但是我宁愿移动按钮.我使用的是普通的 UIBarButtonItem
,两者之间有灵活的空格.
Since this is a custom view, I know that I can change the y position and move it to start at the bottom of safe area but I'd rather move the buttons. I'm using plain UIBarButtonItem
with flexible space in between.
推荐答案
在 iOS 11
中,Apple弃用了顶部和底部布局指南
,并替换为单个安全区域布局指南
.因此,请使用安全区域布局指南
将视图从主页指示器移至上方.
In iOS 11
, Apple is deprecating the top and bottom layout guides
and being replaced with a single safe area layout guide
.
So use Safe Area Layout Guides
to move the view above from the home indicator.
使用故事板:
- 转到
Storyboard
并在Interface Builder文档部分
- 勾选
使用安全区域布局指南
复选框 - 然后将
底部约束
更改为相对于安全区域
- Go to
Storyboard
and in theInterface Builder Document section
- Tick the
Use Safe Area Layout Guides
check box - Then change the
Bottom Constraint
to be relative to theSafe Area
现在,视图在 Home Indicator
上方对齐.
Now the views are aligned above the 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)
])
这篇关于自定义UIToolbar太靠近iPhone X上的主页指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!