简体   繁体   English

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

[英]iOS 8 / XCode 6 autolayout constraints applied after view appears

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时,我注意到autolayout出现了一些奇怪的行为。

I have a storyboard with a view that just has a UIImageView and two UILabels. 我有一个故事板,其视图只有一个UIImageView和两个UILabel。 This view gets loaded using a UiPageViewController. 使用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. UIImageView在视图中水平和垂直居中,并且存在约束,指定两个标签与图像视图的距离。 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. 当故事板和自动布局值发生冲突时(例如,在故事板中,一个标签在图像视图上方20个像素但自动布局显示它应该在40像素以上),过去的自动布局值是在加载视图之前成功应用。 Thus, when the view gets shown for the first time, everything is in the right place. 因此,当视图第一次显示时,一切都在正确的位置。

Now with iOS 8 / XCode 6, I'm seeing that the view loads and then things jump to their final autolayout position. 现在使用iOS 8 / XCode 6,我看到视图加载然后事情跳转到他们的最终自动布局位置。 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. 据我所知,没有代码更改导致此问题。

Where is your code that is making changes to your constraints? 您的代码在哪里更改了约束?

If you're keeping it in viewWillAppear: you will have that problem. 如果你将它保存在viewWillAppear中:你将遇到这个问题。 I found that viewDidLayoutSubviews works well for iOS8 but not always for iOS7 backwards. 我发现viewDidLayoutSubviews适用于iOS8,但并不总是适用于iOS7。

Eventually, if you cant use viewDidLayoutSubviews , what I'd suggest is hide the container view in viewDidLoad and unhide it in viewWillAppear , only after the constraints have been applied. 最后,如果你不能使用viewDidLayoutSubviews ,我建议隐藏容器视图在viewDidLoad中 ,并在viewWillAppear中取消隐藏它,只有在应用了约束之后。 It gets you a small extra delay when loading the screen but the constraints change gets transparent to the user. 加载屏幕时会给您带来一点额外的延迟,但约束更改会对用户透明。

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. 只需将它们放在与控制器视图挂钩的容器视图中就可以解决问题。

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. 所以我只是把标签栏放回去了。

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

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