Xcode 9 的安全区域 [英] Safe Area of Xcode 9

查看:31
本文介绍了Xcode 9 的安全区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索 Xcode9 Beta 时发现 Safe Area 在界面构建器上查看层次结构查看器.很好奇并试图了解 Apples 文档中的 Safe Area,主要是文档说与自动布局直接交互的视图区域" 但它并没有让我满意,我想知道实际用途这个新事物.

有人知道吗?

Apple 文档中安全区域的结论段落.

<块引用>

UILayoutGuide 类旨在执行以前由虚拟视图执行的所有任务,但以更安全、更有效的方式执行.布局指南不定义新视图.它们不参与视图层次结构.相反,他们只是在自己视图的坐标系中定义了一个矩形区域,可以与自动布局进行交互.

解决方案

Safe Area 是一个布局指南():
如果您在代码中创建约束,请使用 UIView 的 safeAreaLayoutGuide 属性来获取相关的布局锚点.让我们在代码中重新创建上面的 Interface Builder 示例,看看它的外观:

假设我们的视图控制器中有绿色视图作为属性:

private let greenView = UIView()

我们可能有一个函数来设置从 viewDidLoad 调用的视图和约束:

private func setupView() {greenView.translatesAutoresizingMaskIntoConstraints = falsegreenView.backgroundColor = .greenview.addSubview(greenView)}

一如既往地使用根视图的 layoutMarginsGuide 创建前导和尾随边距约束:

 让边距 = view.layoutMarginsGuideNSLayoutConstraint.activate([greenView.leadingAnchor.constraint(equalTo: margins.leadingAnchor),greenView.trailingAnchor.constraint(equalTo: margins.trailingAnchor)])

现在,除非您只针对 iOS 11,否则您需要使用 #available 包装安全区域布局指南约束,并回退到早期 iOS 版本的顶部和底部布局指南:

if #available(iOS 11, *) {让指南 = view.safeAreaLayoutGuideNSLayoutConstraint.activate([greenView.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),guide.bottomAnchor.constraintEqualToSystemSpacingBelow(greenView.bottomAnchor,乘数:1.0)])} 别的 {让标准间距:CGFloat = 8.0NSLayoutConstraint.activate([greenView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, 常量:standardSpacing),bottomLayoutGuide.topAnchor.constraint(equalTo:greenView.bottomAnchor,常量:standardSpacing)])}


结果:

<小时>

遵循 UIView 扩展,让您可以轻松地以编程方式使用 SafeAreaLayout.

扩展 UIView {//顶锚var safeAreaTopAnchor: NSLayoutYAxisAnchor {如果#available(iOS 11.0,*){返回 self.safeAreaLayoutGuide.topAnchor} 别的 {返回 self.topAnchor}}//底部锚点var safeAreaBottomAnchor: NSLayoutYAxisAnchor {如果#available(iOS 11.0,*){返回 self.safeAreaLayoutGuide.bottomAnchor} 别的 {返回 self.bottomAnchor}}//左锚var safeAreaLeftAnchor: NSLayoutXAxisAnchor {如果#available(iOS 11.0,*){返回 self.safeAreaLayoutGuide.leftAnchor} 别的 {返回 self.leftAnchor}}//右锚var safeAreaRightAnchor: NSLayoutXAxisAnchor {如果#available(iOS 11.0,*){返回 self.safeAreaLayoutGuide.rightAnchor} 别的 {返回 self.rightAnchor}}}

<小时>

以下是 Objective-C 中的示例代码:

<小时>

这里是 安全区域布局指南


处理 iPhone-X 的用户界面设计需要安全区域.这是如何为 iPhone-X 设计用户界面的基本指南使用安全区域布局

While exploring Xcode9 Beta Found Safe Area on Interface builders View hierarchy viewer. Got curious and tried to know about Safe Area on Apples documentation, in gist the doc says "The the view area which directly interacts with Auto layout" But it did not satisfy me, I want to know Practical use of this new thing.

Do any one have some clue?

Conclusion paragraph from Apple doc for Safe area.

The UILayoutGuide class is designed to perform all the tasks previously performed by dummy views, but to do it in a safer, more efficient manner. Layout guides do not define a new view. They do not participate in the view hierarchy. Instead, they simply define a rectangular region in their owning view’s coordinate system that can interact with Auto Layout.

解决方案

Safe Area is a layout guide (Safe Area Layout Guide).
The layout guide representing the portion of your view that is unobscured by bars and other content. In iOS 11+, Apple is deprecating the top and bottom layout guides and replacing them with a single safe area layout guide.

When the view is visible onscreen, this guide reflects the portion of the view that is not covered by other content. The safe area of a view reflects the area covered by navigation bars, tab bars, toolbars, and other ancestors that obscure a view controller's view. (In tvOS, the safe area incorporates the screen's bezel, as defined by the overscanCompensationInsets property of UIScreen.) It also covers any additional space defined by the view controller's additionalSafeAreaInsets property. If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide always matches the edges of the view.

For the view controller's root view, the safe area in this property represents the entire portion of the view controller's content that is obscured, and any additional insets that you specified. For other views in the view hierarchy, the safe area reflects only the portion of that view that is obscured. For example, if a view is entirely within the safe area of its view controller's root view, the edge insets in this property are 0.

According to Apple, Xcode 9 - Release note
Interface Builder uses UIView.safeAreaLayoutGuide as a replacement for the deprecated Top and Bottom layout guides in UIViewController. To use the new safe area, select Safe Area Layout Guides in the File inspector for the view controller, and then add constraints between your content and the new safe area anchors. This prevents your content from being obscured by top and bottom bars, and by the overscan region on tvOS. Constraints to the safe area are converted to Top and Bottom when deploying to earlier versions of iOS.


Here is simple reference as a comparison (to make similar visual effect) between existing (Top & Bottom) Layout Guide and Safe Area Layout Guide.

Safe Area Layout:

AutoLayout


How to work with Safe Area Layout?

Follow these steps to find solution:

  • Enable 'Safe Area Layout', if not enabled.
  • Remove 'all constraint' if they shows connection with with Super view and re-attach all with safe layout anchor. OR Double click on a constraint and edit connection from super view to SafeArea anchor

Here is sample snapshot, how to enable safe area layout and edit constraint.

Here is result of above changes


Layout Design with SafeArea
When designing for iPhone X, you must ensure that layouts fill the screen and aren't obscured by the device's rounded corners, sensor housing, or the indicator for accessing the Home screen.

Most apps that use standard, system-provided UI elements like navigation bars, tables, and collections automatically adapt to the device's new form factor. Background materials extend to the edges of the display and UI elements are appropriately inset and positioned.

For apps with custom layouts, supporting iPhone X should also be relatively easy, especially if your app uses Auto Layout and adheres to safe area and margin layout guides.


Here is sample code (Ref from: Safe Area Layout Guide):
If you create your constraints in code use the safeAreaLayoutGuide property of UIView to get the relevant layout anchors. Let’s recreate the above Interface Builder example in code to see how it looks:

Assuming we have the green view as a property in our view controller:

private let greenView = UIView()

We might have a function to set up the views and constraints called from viewDidLoad:

private func setupView() {
  greenView.translatesAutoresizingMaskIntoConstraints = false
  greenView.backgroundColor = .green
  view.addSubview(greenView)
}

Create the leading and trailing margin constraints as always using the layoutMarginsGuide of the root view:

 let margins = view.layoutMarginsGuide
    NSLayoutConstraint.activate([
      greenView.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
      greenView.trailingAnchor.constraint(equalTo: margins.trailingAnchor)
 ])

Now unless you are targeting iOS 11 only you will need to wrap the safe area layout guide constraints with #available and fall back to top and bottom layout guides for earlier iOS versions:

if #available(iOS 11, *) {
  let guide = view.safeAreaLayoutGuide
  NSLayoutConstraint.activate([
   greenView.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),
   guide.bottomAnchor.constraintEqualToSystemSpacingBelow(greenView.bottomAnchor, multiplier: 1.0)
   ])

} else {
   let standardSpacing: CGFloat = 8.0
   NSLayoutConstraint.activate([
   greenView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: standardSpacing),
   bottomLayoutGuide.topAnchor.constraint(equalTo: greenView.bottomAnchor, constant: standardSpacing)
   ])
}


Result:


Following UIView extension, make it easy for you to work with SafeAreaLayout programatically.

extension UIView {

  // Top Anchor
  var safeAreaTopAnchor: NSLayoutYAxisAnchor {
    if #available(iOS 11.0, *) {
      return self.safeAreaLayoutGuide.topAnchor
    } else {
      return self.topAnchor
    }
  }

  // Bottom Anchor
  var safeAreaBottomAnchor: NSLayoutYAxisAnchor {
    if #available(iOS 11.0, *) {
      return self.safeAreaLayoutGuide.bottomAnchor
    } else {
      return self.bottomAnchor
    }
  }

  // Left Anchor
  var safeAreaLeftAnchor: NSLayoutXAxisAnchor {
    if #available(iOS 11.0, *) {
      return self.safeAreaLayoutGuide.leftAnchor
    } else {
      return self.leftAnchor
    }
  }

  // Right Anchor
  var safeAreaRightAnchor: NSLayoutXAxisAnchor {
    if #available(iOS 11.0, *) {
      return self.safeAreaLayoutGuide.rightAnchor
    } else {
      return self.rightAnchor
    }
  }

}


Here is sample code in Objective-C:


Here is Apple Developer Official Documentation for Safe Area Layout Guide


Safe Area is required to handle user interface design for iPhone-X. Here is basic guideline for How to design user interface for iPhone-X using Safe Area Layout

这篇关于Xcode 9 的安全区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆