滚动视图的内容在滚动时会改变大小 [英] Content of scrollview changes size when scrolling

查看:112
本文介绍了滚动视图的内容在滚动时会改变大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 6和Xcode 4.6以及Interface Builder,我在控制器中添加了一个 UIScrollView ,它使用了所有可用空间。在这个 UIScrollView 中我添加了一些视图(一个 UIImageView ,一个 UITextView UITableView (我禁用了 UITextView 的滚动和 UITableView ))。

I'm using iOS 6 and Xcode 4.6 and with the Interface Builder I added to my controller a UIScrollView that uses all the available space. In this UIScrollView I added some views (a UIImageView, a UITextView and a UITableView (I disabled the scrolling for the UITextView and the UITableView)).

在:

- (void) viewDidLayoutSubviews;

方法我通过增加高度来调整所有视图的大小(以适应各自的内容)。
然后,我设置了我的 UIScrollView 的contentSize,以便它符合其内容(我的3个视图)。

method I resized all my views by increasing their height (to fit with their respective content). Then, I set the contentSize of my UIScrollView so that it fits with its content (my 3 views).

调整大小有效,我可以在运行时在应用程序中看到它们。
此外,滚动是好的(contentSize是正确的,因为我可以滚动)。

The resizing works and I can see them in my application when I run it. Also, the scrolling is good (the contentSize is correct as I can scroll).

然而,我有一个大问题:我滚动时,我的3个视图被调整为它们的初始大小(而不是保持我通过frame属性分配它们的大小)。

However, I have one big issue : as soon as I scroll, my 3 views are resized to their initial size (instead of keeping the size I assigned them via the frame property).

我的所有视图都通过Interface Builder添加到我的故事板中。

All my views are added with Interface Builder to my storyboard.

你知道我做错了什么吗?为什么我的 UIScrollView的内容在滚动时会重置其大小?

Do you know what I did wrong? Why my UIScrollView's content is reseting its size when I scroll?

感谢您的帮助

推荐答案

您在故事板中启用了自动布局。滚动滚动视图时,自动布局会根据故事板中的约束运行并重置子视图帧。

You have auto layout enabled in your storyboard. When you scroll the scroll view, auto layout runs and resets your subview frames based on the constraints in the storyboard.

有几种方法可以解决此问题。一种方法是关闭故事板中的自动布局。另一种方法是将插座连接到故事板中的约束,并更新约束(而不是设置框架)以使自动布局将视图放在您想要的位置。

There are a few ways to fix this. One way is to turn off auto layout in your storyboard. Another way is to connect outlets to the constraints in your storyboard, and update the constraints (instead of setting the frames) to make auto layout put the views where you want them.

这篇关于滚动视图的内容在滚动时会改变大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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