简体   繁体   English

如何快速为新 iPhone 设备设置安全区域布局

[英]How to set Safe Area Layout for new IPhone Devices in swift

I have created a simple view.Xib file, if internet connection interrupts or disconnects for any reason my view will be displayed at the bottom.我创建了一个简单的 view.Xib 文件,如果 Internet 连接因任何原因中断或断开连接,我的视图将显示在底部。

For this I have set programmatically NSLayoutConstraint .为此,我以编程NSLayoutConstraint设置了NSLayoutConstraint

  • If I use SafeAreaLayout it gives spaces at the bottom of every new iPhone devices like (iPhone XR, 11, Pro or Pro max).如果我使用SafeAreaLayout它会在每个新 iPhone 设备(如 iPhone XR、11、Pro 或 Pro max)的底部SafeAreaLayout出空间。
  • If I don't use SafeAreaLayout then view at bottom not properly displayed.如果我不使用SafeAreaLayout则底部的视图未正确显示。

In all other devices (iPhone 7, 8 or plus) with SafeAreaLayout it works properly.在具有SafeAreaLayout所有其他设备(iPhone 7、8 或 plus)中,它可以正常工作。

How can I set view properly at the bottom without space?如何在没有空间的底部正确设置视图?

My Code:我的代码:

let viewW = offlineView.fromNib()
view.addSubview(viewW)
viewW.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([

viewW.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
viewW.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
viewW.topAnchor.constraint(equalTo: self.view.bottomAnchor,constant: -25),
viewW.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
])

let viewW = offlineView.fromNib()
view.addSubview(viewW)
viewW.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([

viewW.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
viewW.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
viewW.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -25),
viewW.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])

Screenshots attached:附上截图:

SafeArea:安全区:

安全区

Without SafeArea:没有安全区:

没有安全区

Constraints of Red Circle:红圈的限制: 红圈约束

You may want to try this approach...您可能想尝试这种方法...

Constrain the "dark" view:约束“暗”视图:

  • Leading to superview Leading引导到超级视图
  • Trailing to superview Trailing尾随到超级视图尾随
  • Bottom to superview Bottom底部到超级视图底部
  • no Height constraint没有高度限制

Then, add your "circle" view as a subview of darkView, and constrain:然后,将您的“圆圈”视图添加为 darkView 的子视图,并约束:

  • Trailing to darkView Trailing: 4-pts拖尾到 darkView 拖尾:4-pts
  • Top to darkView Top: 4-pts从顶部到黑暗视图顶部:4-pts
  • Bottom to the root-view safe-area Bottom: 4-pts从底部到根视图安全区底部:4-pts

and the label, also a subview of darkView, constrained:和标签,也是darkView的子视图,受约束:

  • Trailing to circle Trailing: 8-pts尾随圈尾:8-pts
  • CenterY to circle CenterY CenterY 到 CenterY 圈

Now...现在...

Auto-layout will keep darkView's Bottom at the bottom of the screen, and darkView's Top 4-pts from Top of circle view.自动布局将使darkView 的底部保持在屏幕底部,而darkView 的顶部距圆形视图顶部4 pts。

Auto-layout will keep circle view's Bottom 4-pts from the Bottom of the view (when there's no soft-home-button) and 4-pts from the Bottom of the safe-area (when there is a soft-home-button).自动布局将保持圆图的底部4点从视图的底部(当没有软家按钮),并从安全区的底部4点(当软家按钮) .

Here's the results -- the yellow dashed line is the Safe-Area bounds.这是结果——黄色虚线是安全区边界。

on iPhone 8:在 iPhone 8 上:

在此处输入图片说明

and on iPhone 11 Pro:在 iPhone 11 Pro 上:

在此处输入图片说明

and how it looks in Storyboard:以及它在 Storyboard 中的外观:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM