iOS 8 上的自动布局问题(代码在 iOS 7 上完美运行) [英] Issue with Auto Layout on iOS 8 (code works perfectly on iOS 7)

查看:16
本文介绍了iOS 8 上的自动布局问题(代码在 iOS 7 上完美运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 auto-layout 构建的应用程序,当我在 iOS 7 设备上运行它时,一切仍然按预期工作,但是当我在 iOS 8 设备上运行它时,我的一些约束有点古怪.

I have an app that was built using auto-layout and everything still works as expected when I run it on my iOS 7 device, however when I run it on an iOS 8 device some of my constraints go a little wacky.

我实际上在另一个应用程序中遇到了类似的问题,但这不是一个大问题.现在,我开始怀疑这是否是 iOS 8 SDK 中的一个错误,或者是否有一种我不知道的在 iOS8 中处理自动布局的新方法.

I actually experienced a similar issue in another app but it wasn't a big issue. Now, I'm starting to wonder if it is a bug in the iOS 8 SDK or if there is a new way of handling auto layout in iOS8 that I'm not aware of.

这是我的设置:我有一个 UIView ,其中嵌入了一个 UIImageView .视图和图像视图都有约束固定在底部、前导和后沿到他们的超级视图,常量 = 0.

Here is my setup: I have a UIView with a UIImageView embedded inside it. Both the view and the image view have constraints pinning their bottom, leading & trailing edges to their superviews with constant = 0.

UIView 的顶部边缘也固定在其父视图上,并且我有一个连接到约束的 IBOutlet,以便以编程方式对其进行调整.UIImageView 有第四个约束,将其高度固定为设备的高度(在我的例子中是 568).

The UIView also has its top edge pinned to its superview and I have an IBOutlet connected to the constraint in order to adjust it programatically. The UIImageView has a fourth constraint fixing its height to the height of the device (in my case 568).

这是我在 iOS 7 中期望/实现的行为:我正在尝试缩小容器视图的高度,并导致图像视图的顶部被切断,而不会改变纵横比(有点像你将图像的顶部裁剪掉了)......这就是为什么我已经固定了图像视图的高度和底部边缘.

Here is the behavior I am expecting/achieving in iOS 7: I am trying to shrink the height of the container view and cause the top of the image view to be cut off w/o the aspect ratio changing (sort of looks like you cropped the top of the image off)...this is why I have pinned the height of the image view and the bottom edge.

iOS8 中实际发生的情况:容器视图按预期缩小(它保持固定在侧面和底部并且高度缩小).UIImageView 的行为就好像它有一个带有 cosntant == 0 的顶部空间约束.不是切断图像的顶部,而是整个图像缩小了.由于我的图像处于 AspectFit 模式,因此两侧也会夹入以保持纵横比相同(但 imageView 本身仍应固定在前缘、后缘和底部边缘).

What actually happens in iOS8: The container view shrinks as expected (it stays pinned to the sides & bottom & the height shrinks). The UIImageView behaves as if it had a top space constraint with cosntant == 0. Instead of cutting off the top of the image, the whole image shrinks down. Since I have the image in AspectFit mode, the sides pinch in as well to keep the aspect ratio the same (but the imageView itself remains pinned to the leading, trailing & bottom edges as it should).

我是如何做到的:我有一个通知从我的代码的另一部分触发,以调整连接到 IBOutlet 的顶部空间约束.它调用的方法真的很简单:

How I do it: I have a notification that fires from another part of my code to adjust the top-space constraint hooked up to the IBOutlet. The method it calls is really quite simple:

- (void) setTopSpaceForContainerView:(NSNotification*) notif{
    containerView_TopSpace.constant = [[notif.userInfo objectForKey:kTopSpace] intValue];
    [self.view setNeedsUpdateConstraints];
    [self.view setNeedsLayout];
}

其他人有类似经历吗?我正在尝试寻找解决方法,但 iOS 8 似乎决心缩小我的图像.

Anyone else have a similar experience? I'm trying to find a workaround, but iOS 8 just seems determined to shrink my image.

非常感谢!

推荐答案

这绝对是 iOS 8 的 bug.我可以通过将 setNeedsLayout 发送到容器视图(图像视图的超级视图)来解决它.

This is definitely an iOS 8 bug. I was able to work around it by sending setNeedsLayout to the container view (the superview of the image view).

我看到您将 setNeedsLayout 发送到 self.view.如果该代码在您的视图控制器中,那么我猜 self.view 不是容器视图.尝试将 setNeedsLayout 直接发送到容器视图.

I see that you're sending setNeedsLayout to self.view. If that code is in your view controller, then I guess self.view is not the container view. Try sending setNeedsLayout to the container view directly.

演示项目在这里.

这篇关于iOS 8 上的自动布局问题(代码在 iOS 7 上完美运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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