视图出现后应用的 iOS 8 / XCode 6 自动布局约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25925058/
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-09-15 05:37:09  来源:igfitidea点击:

iOS 8 / XCode 6 autolayout constraints applied after view appears

iosxcodeautolayoutxcode6uipageviewcontroller

提问by wuc

I have a project that was working just fine with XCode 5 and iOS 7, but when I recently downloaded XCode 6, I noticed some weird behavior with autolayout.

我有一个在 XCode 5 和 iOS 7 上运行良好的项目,但是当我最近下载 XCode 6 时,我注意到自动布局的一些奇怪行为。

I have a storyboard with a view that just has a UIImageView and two UILabels. This view gets loaded using a UiPageViewController. The UIImageView is centered horizontally and vertically in the view and there are constraints specifying the distance of the two labels from the image view. When there is a case when the storyboard and autolayout values conflict (for example, in the storyboard, one label is 20 pixels above the image view but the autolayout says it should be 40 pixels above), it used to be that the autolayout value was successfully applied before the view loaded. Thus, when the view gets shown for the first time, everything is in the right place.

我有一个带有视图的故事板,其中只有一个 UIImageView 和两个 UILabels。此视图使用 UiPageViewController 加载。UIImageView 在视图中水平和垂直居中,并且存在指定两个标签与图像视图的距离的约束。当 storyboard 和 autolayout 的值发生冲突时(例如,在 storyboard 中,一个标签在图像视图上方 20 像素,但自动布局说它应该在上方 40 像素),曾经是自动布局值是在加载视图之前成功应用。因此,当视图第一次显示时,一切都在正确的位置。

Now with iOS 8 / XCode 6, I'm seeing that the view loads and then things jump to their final autolayout position. This jump is noticeable to the end user both in the simulator and on a real device, and it's really annoying. Is there a way to go back to the old behavior? As far as I'm aware, no code has changed to cause this issue.

现在使用 iOS 8 / XCode 6,我看到视图加载,然后事情跳转到它们的最终自动布局位置。无论是在模拟器中还是在真实设备上,最终用户都会注意到这种跳跃,这真的很烦人。有没有办法回到旧的行为?据我所知,没有任何代码更改导致此问题。

回答by Marcelo Ribeiro

Where is your code that is making changes to your constraints?

对约束进行更改的代码在哪里?

If you're keeping it in viewWillAppear:you will have that problem. I found that viewDidLayoutSubviewsworks well for iOS8 but not always for iOS7 backwards.

如果您将其保留在viewWillAppear 中:您将遇到该问题。我发现viewDidLayoutSubviews适用于 iOS8 但并不总是适用于 iOS7 倒退。

Eventually, if you cant use viewDidLayoutSubviews, what I'd suggest is hide the container view in viewDidLoadand unhide it in viewWillAppear, only after the constraints have been applied. It gets you a small extra delay when loading the screen but the constraints change gets transparent to the user.

最后,如果您不能使用viewDidLayoutSubviews,我建议在viewDidLoad 中隐藏容器视图并在viewWillAppear 中取消隐藏它,只有在应用了约束之后。它在加载屏幕时会给你一个小的额外延迟,但约束的变化对用户来说是透明的。

回答by mwright

I had a similar issue and put items into a container view in preparation for hiding and then showing them as suggested in the other answer.

我有一个类似的问题,并将项目放入容器视图中以准备隐藏,然后按照另一个答案中的建议显示它们。

I didn't love that as a solution and ended up not having to do it.

我不喜欢它作为解决方案,最终不必这样做。

Simply putting them in a container view that was pegged to the controller's view stopped the issue from happening.

只需将它们放在与控制器视图挂钩的容器视图中即可阻止问题发生。

回答by dcotter

For me it was an issue with the tab bar in my project. I didn't need it in that view and I hid it on push. So when the view got pushed onto the stack the bar was hidden and the constraints adjusted accordingly. So I just put the tab bar back in.

对我来说,这是我项目中标签栏的问题。在那个视图中我不需要它,我在推送时隐藏了它。因此,当视图被推入堆栈时,条被隐藏,并且约束相应地进行了调整。所以我只是把标签栏放回去了。