ios - 自定义 UIToolbar 太靠近 iPhone X 上的主页指示器

标签 ios swift iphone-x

我有一个自定义的 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)

按钮太靠近主页指示器 Buttons are too close to the home indicator

This is what I want it to look like (Mail app)

这就是我想要的样子(邮件应用程序)^

由于这是一个自定义 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)
        ])

请参阅这篇文章以获取 Positioning Content Relative to the Safe Area

关于ios - 自定义 UIToolbar 太靠近 iPhone X 上的主页指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236375/

相关文章:

iphone - "_OBJC_CLASS_$_GDataOAuthAuthentication",引用自 :

ios - 使用 Xamarin.Forms 应用填充 iPhone X 屏幕

ios - 如何保持 UITableView 单元格处于选中状态

ios - Swift 无法将类型 '()' 的值分配给类型 'String?'

arrays - Swift 数组行为

ios - Youtube 帮助播放器查看 iOS iPhone X 布局问题

ios - 无论框架的 x 和 y 为何,UIView 始终位于屏幕底部 - Swift

iOS 通用链接打开应用程序,不触发应用程序委托(delegate)方法

ios - 启用模糊时 UIImage 不会淡入/淡出

iphone - jQuery openDatabase() 问题 : variable db is undefined