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

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

问题描述

我正在构建一个新应用程序,并希望在开始时有一个欢迎演练",其中我有一个故事板,其中包含一系列图像,显示在 UIPageViewController 中.我让它加载图像,所有这些都很好,但是只要图像超出上一个"或下一个"视图控制器,就会调整图像大小.我正在使用 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

我已经尝试了所有不同的视图模式(纵横比拟合、纵横比填充、重绘等),它们的行为都相同.

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.只有在动画完成后,视图控制器才会获得 topLayoutGuide.length 的正值.是的,页面视图控制器应该比这更聪明一点,但事实并非如此.
您可以停止使用顶部布局指南并相对于其父视图的顶部进行自动布局约束.或者您可以继续使用顶部布局指南,但要考虑它的 length 何时为 0.您可以通过为情节提要的 NSLayoutConstraint 创建一个出口并在包含 UIImageViewViewController 中覆盖 viewWillLayoutSubviews() 来做到这一点>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.

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

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