SizeToFit UIScrollView 的内容大小
UIView 有一个 SizeToFit 方法,可以使 UIView 适合它的所有子视图。有没有类似的东西,它只会返回它计算的大小,而不修改任何视图的框架。
我在 UIScrollView 上有几个子视图,我想在滚动视图的 contentSize 上执行 SizeToFit,而不是它的框架。我必须在 contentSize 上执行此操作,因为我不想增加 UIScrollView 的“实际”大小,并且内容是动态异步加载的,因此当我将子视图添加到 UIScrollView 时我无法手动执行此操作。
A UIView has a SizeToFit method that will make the UIView fit all of it's subviews. Is there anything like that, which will just return the size that it calculates and not modify any view's frame.
I have several subviews on a UIScrollView and I want to do SizeToFit on the scroll view's contentSize, rather than it's frame. I have to do it on the contentSize, because I don't want to increase the "real" size of the UIScrollView, and the content is loaded dynamically and asynchronously so I can't do it manually when I add the subviews to the UIScrollView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
目前,这是我最好的:
At the moment, this is the best I have:
如果您的函数被频繁调用,您不会希望每次都迭代子视图。相反,每当添加子视图时,都会将当前 contentSize 和新子视图的框架进行并集。
If your function is called frequently, you wouldn't want to iterate over the subviews each time. Instead, whenever a subview is added, do a union of the current contentSize and the frame of the new subview.
在这里找到了解决方案 ScrollView ContentSize,您将能够找到子视图并查找组合内容大小以及表示子视图是水平还是垂直排列的选项。
以下是上面链接中的代码:
另请务必查看博客页面上的评论,因为提供了替代解决方案。
-阿努普
Found a solution here ScrollView ContentSize, you will be able to find the sizes of the subviews and find the combined content size and also options to say whether subviews are arranged horizontally or vertically.
Here is the code from the link above :
Also make sure to have a look at the comments on the blog page as an alternative solution is provided.
-anoop
我已经制作了一个用于设置滚动视图内容大小的函数,请尝试并享受它
首先编写此函数...
然后在添加所有元素后,只需调用此函数并将您的滚动视图作为参数传递...
我100%确定它会起作用............
I have made one function for setting content size of scrollView try it and enjoy
First of all write this function...
then after adding all elements just call this function and pass your scroll view as argument.....
I am 100% sure that it will work.........
这是 Swift 3.0 的答案
Here is the answer for Swift 3.0
虽然 William 的回答非常有用,但它并不能完美工作,因为滚动条也是滚动视图的子视图。事实证明,诀窍是在计算内容大小之前隐藏它们。 Swift 3 代码如下所示:
您还需要在项目中的某处添加以下扩展:
While William's answer was very useful, it didn't work perfectly since the scrollbars are also subviews of the scrollview. Turns out, the trick is to hide them before calculating the content size. Here's what the Swift 3 code would look like:
You'll also need to add the following extension somewhere in your project:
将一个 UIView 放入具有相同宽度和高度的 UIScrollView 中,并将所有子视图添加到 IT 而不是滚动视图。然后将滚动视图上的clipToBounds属性设置为TRUE,并在UIView上调用sizeToFit。您还可以将 UIView 背景的不透明度设置为 0,使其不可见,但仍显示其子视图。
Put a UIView into the UIScrollView that has the same width and height and add all subviews to IT rather than the scrollview. Then set the clipToBounds property on the scrollview to TRUE, and call sizeToFit on your UIView. You can also set the opacity of the UIView's background to 0 making it invisible, but still showing its subviews.
在 swift 2.1 中,将其添加到您的视图控制器中:
In swift 2.1, add this to your view controller:
联合框架大小来计算内容大小是不够的。因为有时,您的子视图需要重叠,有时子视图之间有边距。您必须了解并计算子视图的起源。
联合方法为 A 和 B 设计提供相同的内容。但是如果您知道最大的最后一个子视图,您可以计算正确的内容高度:
此扩展可以计算真实内容高度。
It's not enough that union frame sizes to calculate the content size. Because sometimes, your subview needs to overlap, and sometimes you have margins between subviews. You have to know and calculate origin of subviews.
Union approach gives same content hight for A and B designs. But if you know biggest last subview, you can calculate right content hight:
This extension can calculate the real content height.
Swift 2 扩展:
用法:
scrollView.contentResize
scrollView.contentWidthResize
scrollView.contentHeightResize
Swift 2 extension:
Usage:
scrollView.contentResize
scrollView.contentWidthResize
scrollView.contentHeightResize