UIPmageView在UIPageViewController中调整大小问题 [英] UIImageView resizing issue in UIPageViewController

查看:125
本文介绍了UIPmageView在UIPageViewController中调整大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个新的应用程序,并希望在开头有一个欢迎演练,其中我有一个故事板,其中包含一系列在UIPageViewController中呈现的图像。我有它加载图像和所有这些就好了,但是只要它们超出前一个或下一个ViewController,图像就会被调整大小。我正在使用 Swift 进行开发。

I'm building a new app and wish to have a "Welcome walkthrough" at the beginning wherein I have a storyboard with a series of images presented in a UIPageViewController. I have it loading the images and all of that just fine, however the images are resized whenever they go beyond being the "previous" or "next" ViewController. I am using Swift to develop.

以下是该问题的视频: http://youtu.be/dXcjjT-8Bk0

Here is a video of the issue: http://youtu.be/dXcjjT-8Bk0

我试过所有不同的视图模式(纵横比,纵横填充,重绘等)和它们都表现相同。

I have tried all of the different View Modes (Aspect fit, aspect fill, redraw etc.) and they all behave the same.

我正在使用Auto-Layout + Size Classes简化不同屏幕尺寸的开发。我目前的限制使UIImage显示的大小合适:

I am using Auto-Layout + Size Classes as I wish to simplify the development for different screen sizes. The current constraints I have that make the UIImage appear at the right size are:

Align Centre X  to Superview
Top Space to Top Layout Guide
Bottom Space to Bottom Layout Guide + Equals: 50

我是目前正在使用 Aspect Fit 给我正确的图像(在他们完成'调整大小行为'之后。

I am currently using Aspect Fit which gives me the correct image (after they have done their 'resizing behaviour'.

可以任何人都指导我如何解决这个问题?

Can anyone guide me further as to how to fix this?

推荐答案

从你的视频中,我注意到你的 UIImageView 总是在顶部调整大小,而不是在底部。这肯定是因为你的自动布局限制你称之为顶部空间到顶部布局指南。当你的 UIImageView 的视图控制器正在通过滚动页面视图控制器进行转换,它不知道顶部布局指南的位置,所以它的 topLayoutGuide.length 0 。仅在动画完成之后es视图控制器是否为 topLayoutGuide.length 获得正值。是的,页面视图控制器应该比这更智能,但事实并非如此。

您可以停止使用顶部布局指南并相对于其超级视图的顶部进行自动布局约束。或者您可以继续使用顶部布局指南,但帐户的时间长度 0 。您可以通过在您的 NSLayoutConstraint 创建一个插座并覆盖 viewWillLayoutSubviews()来实现此目的> ViewController 包含 UIImageView s:

From your video, I noticed that your UIImageView is always "resized" at the top, not at the bottom. This is most certainly because of your autolayout constraint you call "Top Space to Top Layout Guide". While your UIImageView's view controller is being transitioned through your scrolling page view controller, it doesn't know where the top layout guide is, so its topLayoutGuide.length is 0. Only after the animation completes does the view controller get a positive value for topLayoutGuide.length. Yes, the page view controller should be a bit smarter than this, but it's not.
You can either stop using the top layout guide and make an autolayout constraint relative to the top of its superview. Or you can continue to use the top layout guide but account for when it's length is 0. You can do this by making an outlet for your storyboard's NSLayoutConstraint and overriding viewWillLayoutSubviews() in your ViewController containing your UIImageViews:

@IBOutlet weak var topSpaceToTLG: NSLayoutConstraint!
var parentTLGlength: CGFloat = 20

override func viewWillLayoutSubviews() {
    if self.topLayoutGuide.length == 0 {
        // Lengthen the autolayout constraint to where we know the 
        // top layout guide will be when the transition completes
        topSpaceToTLG.constant = parentTLGlength
    } else {
        topSpaceToTLG.constant = 0
    }
}

这将始终放在 UIImageView的顶部在顶部布局指南中,假设状态栏始终为 20 点。在布置子视图之前,它将检查顶部布局指南长度是否为 0 ,并相应地调整自动布局约束。过渡动画完成后,将再次触发布局,并且顶部布局指南长度将为预期值,因此约束常量可以返回到 0 。比硬编码更好的是在初始化期间传递父视图控制器的确切长度,考虑到顶部布局指南的任何可能的更改,如添加导航栏。

This will always put the top of your UIImageView at the top layout guide, assuming that the status bar is always 20 points. Before laying out subviews, it will check to see if the top layout guide length is 0 or not and adjusts your autolayout constraint accordingly. After the transition animation completes, layout is triggered again, and the top layout guide length will be the expected value, so the constraint constant can go back to 0. Even better than hardcoding the value is to pass in the parent view controller's exact length during initialization, accounting for any possible changes to the top layout guide like adding a navigation bar.

这篇关于UIPmageView在UIPageViewController中调整大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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