界面生成器:UIView 的布局 iOS 6/7 Delta 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17794037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:27:15  来源:igfitidea点击:

Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?

ioslayoutuiviewxcode5

提问by Frederick C. Lee

I just noticed the iOS 6/7 Delta property found under the UIView's structs layout.

我刚刚注意到在 UIView 的结构布局下找到的 iOS 6/7 Delta 属性。

What is this for and why is this missing from AutoLayout?

这是做什么用的,为什么 AutoLayout 中没有这个?

enter image description here

在此处输入图片说明

采纳答案by digarok

This actually refers to the Delta between layout position from iOS6 to iOS7.

这实际上是指从 iOS6 到 iOS7 布局位置之间的 Delta。

In iOS7, some views can hide the status bar or have it transparent and, in effect, it is overlaid on top of your view. So if you put a UI element at (0.0, 0.0) on iOS6, it will appear below the status bar, but on iOS7 it would appear partially covered underneath the status bar. So in that case you would want a delta that matches the status bar height (20.0 points) so that the layout looks the same in iOS6 and iOS7.

在 iOS7 中,某些视图可以隐藏状态栏或使其透明,实际上,它覆盖在您的视图之上。因此,如果您在 iOS6 上将 UI 元素放在 (0.0, 0.0) 处,它会显示在状态栏下方,但在 iOS7 上,它会显示部分覆盖在状态栏下方。因此,在这种情况下,您需要一个与状态栏高度(20.0 点)匹配的增量,以便在 iOS6 和 iOS7 中布局看起来相同。

I believe this isn't needed if you use autolayout, but of course, then you lose iPad1 support, which many of us aren't willing to concede at this point in time.

我相信如果您使用自动布局,则不需要这样做,但是当然,您将失去对 iPad1 的支持,我们中的许多人目前都不愿意承认这一点。

回答by WDUK

Note: I noticed this question a while ago, but I'm only posting my answer now because the NDA has been lifted

注意:我不久前注意到这个问题,但我现在才发布我的答案,因为 NDA 已取消

Why does it not appear for AutoLayout?

为什么它不会出现在 AutoLayout 中?

As you may have noticed, iOS 7 brings about a whole new look. The look of UI elements have changed, but also so have some of their sizes (or metrics in general). This can make interface design to accommodate both iOS 7 and it's predecessors a bit of a pain.

您可能已经注意到,iOS 7 带来了全新的面貌。UI 元素的外观发生了变化,但它们的一些大小(或一般指标)也发生了变化。这可以使界面设计同时适应 iOS 7 和它的前辈们有点痛苦。

Apple's official line is to use AutoLayout to solve this; this should take a lot of the hassle out of laying out UI elements for you. Sometimes incorporating this is not easily done, especially if you must still support iOS 5 for business reasons, or your interfaces are managed in a way that makes implementing AutoLayout difficult. As such, Apple seems to have provided a way to make your job a bit easier if you fall into this niche category, and they've called this iOS 6/7 Deltas.

苹果官方的路线是使用AutoLayout来解决这个问题;这应该会减少为您布置 UI 元素的很多麻烦。有时合并这并不容易,特别是如果出于业务原因您仍然必须支持 iOS 5,或者您的界面的管理方式使实现 AutoLayout 变得困难。因此,如果你属于这个小众类别,Apple 似乎提供了一种让你的工作更轻松的方法,他们称之为 iOS 6/7 Deltas。

Ok then, what does it do?

那么,它有什么作用呢?

Whilst the label in Interface Builder is a bit unclear as to what 'Delta' means in this context, the code contained in the .xib file that corresponds to this feature is a bit more clear:

虽然 Interface Builder 中的标签对于“Delta”在此上下文中的含义有点不清楚,但与此功能对应的 .xib 文件中包含的代码更清楚一些:

<inset key="insetFor6xAndEarlier" minX="-50" minY="-100" maxX="-50" maxY="300"/>

<inset key="insetFor6xAndEarlier" minX="-50" minY="-100" maxX="-50" maxY="300"/>

The key name insetFor6xAndEarlierexplicitly states what this does; you can provide alternative insets for UI elements when run on iOS 7's predecessors. For example, the above defines the following delta change:

键名insetFor6xAndEarlier明确说明了它的作用;在 iOS 7 的前辈上运行时,您可以为 UI 元素提供替代插图。例如,上面定义了以下增量变化:

x: 50
y: 100
width: -100
height: 200

Whilst the values stored in the .xib doesn't correspond to the quoted values directly, there is a correlation between them.

虽然存储在 .xib 中的值不直接对应于引用的值,但它们之间存在相关性。

x: -minX
y: -minY
width: minX + maxX
height: minY + maxY

The images below shows this change visually. It's quite an extreme example, but it's to demonstrate its ability. I would only expect in practice to have delta changes of only a few pixels.

下图直观地显示了这种变化。这是一个相当极端的例子,但它是为了展示它的能力。我只希望在实践中只有几个像素的增量变化。

iOS7 View

iOS7 View

iOS6 View

iOS6 View

You may notice that the values are the inverse for the iOS 6 view; this is because the deltas are relative to the type of view you're working with. If you're editing for iOS 6, the deltas there are in order to transform the element correctly for iOS 7 (the reverse of the example above).

您可能会注意到,对于 iOS 6 视图,这些值是相反的;这是因为增量与您正在使用的视图类型相关。如果您正在为 iOS 6 进行编辑,则存在的增量是为了为 iOS 7 正确转换元素(与上例相反)。

In order to view the different styles, you can change the way Interface Builder presents it based on the OS it would be running on. This is contained within the File Inspector->Interface Builder Document (1st tab on the right bar), as so:

为了查看不同的样式,您可以根据它将运行的操作系统更改 Interface Builder 的显示方式。这包含在 File Inspector->Interface Builder Document(右侧栏上的第一个选项卡)中,如下所示:

Interface Style Switch

Interface Style Switch

Does this exist if I like to code my interface by hand?

如果我喜欢手动编写我的界面,这是否存在?

Not directly, but you can easily achieve the same effect by having conditional checks on OS version within your code, and setting the correct position/size accordingly. The delta ability exists in Interface Builder because there would be no straightforward way to have conditional positioning without having code to do it, and the point of Interface Builder is to get a much code out of the way as possible for UI.

不是直接的,但是您可以通过在代码中对操作系统版本进行条件检查并相应地设置正确的位置/大小来轻松实现相同的效果。Interface Builder 中存在 delta 能力,因为没有直接的方法可以在没有代码的情况下进行条件定位,而 Interface Builder 的重点是为 UI 尽可能多地编写代码。

Overall...

总体...

Apple strongly recommend that you use AutoLayout, it makes your life easier in most cases. If you can't use it (for reasons mentioned above), deltas provide you with the flexibility to position your UI elements appropriately, based on the current OS's metrics, without the need to manually reposition them in code. A good example is to adjust for the lack of status bar, but there are plenty of other use cases.

Apple 强烈建议您使用 AutoLayout,它在大多数情况下会让您的生活更轻松。如果您不能使用它(出于上述原因),增量为您提供了根据当前操作系统的指标适当定位 UI 元素的灵活性,而无需在代码中手动重新定位它们。一个很好的例子是调整状态栏的缺失,但还有很多其他用例。

Naturally, if you're only developing for iOS7 and above, you don't need to know this feature/won't discover it. Only if you need to have iOS6 devices running your application when built with the iOS7 SDK, without autolayout, do you need deltas.

当然,如果你只为 iOS7 及以上版本开发,你不需要知道这个特性/不会发现它。只有在使用 iOS7 SDK 构建时需要让 iOS6 设备运行您的应用程序时,没有自动布局,您才需要增量。

At the time of writing (21st August), I can't find any documentation regarding this feature, nor any mentions in the WWDC material. I've had a play around, and after a bit of research, that is what I've discovered.

在撰写本文时(8 月 21 日),我找不到有关此功能的任何文档,也找不到 WWDC 材料中的任何提及。我玩了一会儿,经过一些研究,这就是我发现的。

回答by Saran

I know this is already been answered, just adding a small variant hoping it could also help those who don't use auto layout and still want to support iOS 6.1 and earlier versions.

我知道这已经得到了回答,只是添加了一个小的变体,希望它也可以帮助那些不使用自动布局但仍希望支持 iOS 6.1 及更早版本的人。

Read this Apple's Transition Guide - Supporting earlier version

阅读此Apple 的过渡指南 - 支持早期版本

Choose 'View as' to 'iOS 7.0 and Later'

选择“查看方式”到“iOS 7.0 及更高版本”

enter image description here

enter image description here

Base UI for iOS 7. For iOS 6 give suitable delta value. Use preview to see how this will render in iOS 7 and iOS 6 device.

iOS 7 的基本 UI。对于 iOS 6,给出合适的增量值。使用预览查看这将如何在 iOS 7 和 iOS 6 设备中呈现。

enter image description here

enter image description here

Quick steps:

快速步骤:

Select each immediate children of root view individually and add 20px to its 'Y' value.

分别选择根视图的每个直接子级,并将 20px 添加到其“Y”值。

enter image description here

enter image description here

Then, select all immediate children collectively and give delta Y as -20px. You can also do this in batch or individually.

然后,共同选择所有直接子级,并将 delta Y 指定为 -20px。您也可以批量或单独执行此操作。

enter image description here

enter image description here

回答by sunkehappy

AutoLayout requires at least iOS 6.0. If you want to support iOS 5.0 you couldn't use AutoLayout.

AutoLayout 至少需要 iOS 6.0。如果您想支持 iOS 5.0,则不能使用 AutoLayout。

And those deltas are used to help you adjust the view position on different iOS version(mainly iOS 7 and iOS version lower than 7).

这些deltas用于帮助您调整不同iOS版本(主要是iOS 7和低于7的iOS版本)的视图位置。

I use those value to help me like this picture. enter image description here

我用这些价值来帮助我喜欢这张照片。 enter image description here

回答by onmyway133

To see iOS 6/7 Delta in action, I will demo with a SegmentedControl that appears properly on both iOS 6 and iOS 7 devices.

要查看 iOS 6/7 Delta 的实际运行情况,我将使用在 iOS 6 和 iOS 7 设备上正确显示的 SegmentedControl 进行演示。

First, select your .Xib or ViewController in Storyboard. Uncheck Use Autolayoutand select "View as iOS 7 and later"

首先,在 Storyboard 中选择您的 .Xib 或 ViewController。取消选中Use Autolayout并选择“ View as iOS 7 and later

enter image description here

enter image description here

In the Interface Builder canvas, place your SegmentedControl so that its origin.yis 20. In iOS 6/7 Delta, choose -20 for DeltaY

在 Interface Builder 画布中,放置 SegmentedControl 使其origin.y为 20。在 iOS 6/7 Delta 中,为 DeltaY 选择 -20

enter image description here

enter image description here

This will make your the SegmentedControl laid below the Status Bar in both iOS 6 and iOS 7 devices

这将使您的 SegmentedControl 位于 iOS 6 和 iOS 7 设备中的状态栏下方

enter image description hereenter image description here

enter image description hereenter image description here

Another useful quotes from Developer's Guide to the iOS 7 Status Bar

来自iOS 7 状态栏开发者指南的另一个有用的引用

Deltas can be set individually for each view and work as you would expect. If your storyboard or nib is set to view as iOS 6, then setting the deltas will cause that view to be shifted and/or resized by the set delta amount when run in iOS 7. Alternately, if your storyboard or nib is set to view in iOS 7, then the deltas will be applied when run in iOS 6

可以为每个视图单独设置增量,并按您的预期工作。如果您的故事板或笔尖设置为在 iOS 6 中查看,那么在 iOS 7 中运行时,设置增量将导致该视图按设置的增量量移动和/或调整大小。或者,如果您的故事板或笔尖设置为查看在 iOS 7 中,那么在 iOS 6 中运行时将应用增量

回答by Marcelo dos Santos

If you are using AutoLayout, then Delta is not available. Try this (tested in iPhone 4s running iOS6):

如果您使用 AutoLayout,则 Delta 不可用。试试这个(在运行 iOS6 的 iPhone 4s 中测试):

- (void) viewWillLayoutSubviews {

//iOS 6 workaround offset
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {

    self.view.clipsToBounds = YES;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenHeight = 0.0;
    screenHeight = screenRect.size.width;
    CGRect screenFrame = CGRectMake(0, -20, self.view.frame.size.width,self.view.frame.size.height+10);

    self.view.frame = screenFrame;
}
}