ios 如何自动调整 UIScrollView 的大小以适合其内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2944294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I auto size a UIScrollView to fit its content
提问by jwerre
Is there a way to make a UIScrollView
auto-adjust to the height (or width) of the content it's scrolling?
有没有办法UIScrollView
自动调整它滚动的内容的高度(或宽度)?
Something like:
就像是:
[scrollView setContentSize:(CGSizeMake(320, content.height))];
回答by leviathan
The best method I've ever come across to update the content size of a UIScrollView
based on its contained subviews:
我遇到的最好的方法是UIScrollView
根据其包含的子视图更新内容大小:
Objective-C
目标-C
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
Swift
迅速
let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = contentRect.size
回答by emenegro
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself
UIScrollView 不会自动知道其内容的高度。您必须自己计算高度和宽度
Do it with something like
用类似的东西做
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in scrollView.subviews)
{
scrollViewHeight += view.frame.size.height;
}
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
But this only work if the views are one below the other. If you have a view next to each other you only have to add the height of one if you don't want to set the content of the scroller larger than it really is.
但这仅在视图低于另一个时才有效。如果您有一个彼此相邻的视图,如果您不想将滚动条的内容设置为比实际大,则只需添加一个的高度。
回答by Adam Waite
Solution if you're using auto layout:
如果您使用自动布局的解决方案:
Set
translatesAutoresizingMaskIntoConstraints
toNO
on all views involved.Position and size your scroll view with constraints external to the scroll view.
Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll viewand do not rely on the scroll view to get their size.
在所有涉及的视图上设置
translatesAutoresizingMaskIntoConstraints
为NO
。使用滚动视图外部的约束来定位和调整滚动视图的大小。
使用约束在滚动视图中布置子视图,确保约束与滚动视图的所有四个边缘相关并且不依赖滚动视图来获取它们的大小。
Source: https://developer.apple.com/library/ios/technotes/tn2154/_index.html
来源:https: //developer.apple.com/library/ios/technotes/tn2154/_index.html
回答by richy
I added this to Espuz and JCC's answer. It uses the y position of the subviews and doesn't include the scroll bars. EditUses the bottom of the lowest sub view that is visible.
我将此添加到 Espuz 和 JCC 的答案中。它使用子视图的 y 位置,不包括滚动条。编辑使用可见的最低子视图的底部。
+ (CGFloat) bottomOfLowestContent:(UIView*) view
{
CGFloat lowestPoint = 0.0;
BOOL restoreHorizontal = NO;
BOOL restoreVertical = NO;
if ([view respondsToSelector:@selector(setShowsHorizontalScrollIndicator:)] && [view respondsToSelector:@selector(setShowsVerticalScrollIndicator:)])
{
if ([(UIScrollView*)view showsHorizontalScrollIndicator])
{
restoreHorizontal = YES;
[(UIScrollView*)view setShowsHorizontalScrollIndicator:NO];
}
if ([(UIScrollView*)view showsVerticalScrollIndicator])
{
restoreVertical = YES;
[(UIScrollView*)view setShowsVerticalScrollIndicator:NO];
}
}
for (UIView *subView in view.subviews)
{
if (!subView.hidden)
{
CGFloat maxY = CGRectGetMaxY(subView.frame);
if (maxY > lowestPoint)
{
lowestPoint = maxY;
}
}
}
if ([view respondsToSelector:@selector(setShowsHorizontalScrollIndicator:)] && [view respondsToSelector:@selector(setShowsVerticalScrollIndicator:)])
{
if (restoreHorizontal)
{
[(UIScrollView*)view setShowsHorizontalScrollIndicator:YES];
}
if (restoreVertical)
{
[(UIScrollView*)view setShowsVerticalScrollIndicator:YES];
}
}
return lowestPoint;
}
回答by Josh
Here is the accepted answer in swift for anyone who is too lazy to convert it :)
对于那些懒得转换它的人来说,这是 swift 中公认的答案:)
var contentRect = CGRectZero
for view in self.scrollView.subviews {
contentRect = CGRectUnion(contentRect, view.frame)
}
self.scrollView.contentSize = contentRect.size
回答by fredericdnd
Here's a Swift 3 adaptation of @leviatan's answer :
这是@leviatan 答案的 Swift 3 改编版:
EXTENSION
延期
import UIKit
extension UIScrollView {
func resizeScrollViewContentSize() {
var contentRect = CGRect.zero
for view in self.subviews {
contentRect = contentRect.union(view.frame)
}
self.contentSize = contentRect.size
}
}
USAGE
用法
scrollView.resizeScrollViewContentSize()
Very easy to use !
非常容易使用!
回答by gokhanakkurt
Following extension would be helpful in Swift.
以下扩展将有助于Swift。
extension UIScrollView{
func setContentViewSize(offset:CGFloat = 0.0) {
// dont show scroll indicators
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
var maxHeight : CGFloat = 0
for view in subviews {
if view.isHidden {
continue
}
let newHeight = view.frame.origin.y + view.frame.height
if newHeight > maxHeight {
maxHeight = newHeight
}
}
// set content size
contentSize = CGSize(width: contentSize.width, height: maxHeight + offset)
// show scroll indicators
showsHorizontalScrollIndicator = true
showsVerticalScrollIndicator = true
}
}
Logic is the same with the given answers. However, It omits hidden views within UIScrollView
and calculation is performed after scroll indicators set hidden.
逻辑与给出的答案相同。但是,它省略了其中的隐藏视图,UIScrollView
并且在滚动指示器设置为隐藏后执行计算。
Also, there is an optional function parameter and you're able to add an offset value by passing parameter to function.
此外,还有一个可选的函数参数,您可以通过将参数传递给函数来添加偏移值。
回答by othierry42
Great & best solution from @leviathan. Just translating to swift using FP (functional programming) approach.
来自@leviathan 的最佳解决方案。只需使用 FP(函数式编程)方法转换为 swift。
self.scrollView.contentSize = self.scrollView.subviews.reduce(CGRect(), {
CGRectUnion(float maxHeight = 0;
for (UIView *child in scrollView.subviews) {
float childHeight = child.frame.origin.y + child.frame.size.height;
//if child spans more than current maxHeight then make it a new maxHeight
if (childHeight > maxHeight)
maxHeight = childHeight;
}
//set content size
[scrollView setContentSize:(CGSizeMake(320, maxHeight))];
, .frame)
}.size
回答by paxx
You can get height of the content inside UIScrollView by calculate which child "reaches furthers". To calculate this you have to take in consideration origin Y (start) and item height.
您可以通过计算哪个孩子“进一步到达”来获取 UIScrollView 中内容的高度。为了计算这个你必须要考虑到原点Y(启动)和项目的高度。
NSInteger maxY = 0;
for (UIView* subview in scrollView.subviews)
{
if (CGRectGetMaxY(subview.frame) > maxY)
{
maxY = CGRectGetMaxY(subview.frame);
}
}
maxY += 10;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, maxY)];
By doing things this way items (subviews) don't have to be stacked directly one under another.
通过这种方式,项目(子视图)不必直接堆叠在另一个下面。
回答by Chris
I came up with another solution based on @emenegro's solution
我想出了另一个基于@emenegro 的解决方案的解决方案
##代码##Basically, we figure out which element is furthest down in the view and adds a 10px padding to the bottom
基本上,我们找出视图中哪个元素最靠下,并在底部添加一个 10px 的填充