ios - UIScrollView 内容大小在不应该的时候裁剪 subview

标签 ios uiview uiscrollview layoutsubviews contentsize

我有一个 UIScrollView,其中包含几个动态调整大小的 subview 。我可以很好地调整 subview 的大小和布局,但是当我设置 ScrollView 本身的内容大小时,底部的 subview 会被剪裁。为什么 ScrollView 的内容大小高度应该大于它包含的 View 高度的总和?

这是我的详细情况: 我有一个 super View ,其中包含一个包含多个 subview 的 UIScrollView。在 superview 的 layoutSubviews 方法中,我计算了每个 subview 所需的大小,然后设置框架,使 subview 垂直向下平铺在屏幕上,它们之间有一点空间。完成后,我将 UIScrollView 内容大小的高度设置为最后一个 subview 的结尾 (origin.y + size.height)。理论上,这意味着 ScrollView 内容区域的底部应该与最后一个 subview 的底部完全对齐。

但事实并非如此。相反,最后一个 subview 的一大块被剪掉了。它仍然在那里 - 如果我向下滚动,我可以在“弹跳”期间看到剩余部分。这个问题在横向模式下更严重——底部 subview 的很大一部分根本不可见。

subview 都已正确排列和定位。问题是 UIScrollView 的 contentSize 似乎需要明显大于 subview 高度的总和(加上它们之间的空间)。这对我来说没有任何意义。此外,大小“关闭”的数量各不相同 - 我多次使用不同的 subview 重复使用此 View ,并且它们都关闭了不同的数量。因此,简单地向内容 View 高度添加一个常量将无济于事。

是什么导致内容大小(或我的高度计算)无法正常运行?

代码:

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat width = self.bounds.size.width - [self subviewLeftMargin] - [self subviewRightMargin]; // All subviews have same width as parent view

    CGFloat x = [self subviewLeftMargin]; // All subviews should start at the far left of the view 
    CGFloat y = [self spaceBetweenSubviews]; // Running tally of the y coordinate for the next view

    /* Adjust the subviews */
    for(UIView *view in self.theVariousSubviews) {  
        /* Resize the view with the desired width, then let it size its height as needed */
        view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, width, view.frame.size.height);
        CGSize newSize = [view sizeThatFits:view.frame.size];

        /* Set the origin */
        //The subviews are positioned correctly, so this doesn't seem to be a problem
        view.frame = CGRectMake(x, y, newSize.width, newSize.height);

        /* Have the view refresh its own layout */
        [view setNeedsLayout];

        /* Update the y value for the next subview */
        y += newSize.height + [self spaceBetweenSubviews];
    }

    /* Resize the scroll view to ensure it fits all of the content */
    CGFloat scrollViewHeight = y;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, scrollViewHeight);
    //Content size is set to the same total height of all subviews and spacing, yet it is too small. Why?
}

最佳答案

您好,在我看来,您的计算和调整大小的时间是错误的。 如果没有布局更改的缺失代码,我无法完全理解这个问题。

令我印象深刻的是,您分配了两次 view.frame,并且在新计算之间您拦截了带有子布局的进程,这可能会改变您的计算所依赖的某些值。

我只能建议您将计算与布局分开,而不是在计算时调用方法。为了使它更清晰,您应该删除一个缺少计算的示例应用程序,或者为您自己添加一些 NSLog 语句,显示任何 subview 的帧原点大小和 ScrollView 的 contentOffset。 根据我的经验, ScrollView 通常可以正常工作,因此我希望您的代码中存在错误。

关于ios - UIScrollView 内容大小在不应该的时候裁剪 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9455132/

相关文章:

ios - 同步 UISlider 和 UIScrollView

ios - 如何制作只显示当前消息的 iMessage 应用程序?

ios - 用切出的圆圈遮盖 UIView

objective-c - 在 ViewController 中的两个 View 上以编程方式设置自动布局约束

ios - 如何暂停uiview动画?

ios - UIPageViewController 内的 UITableViewController,如何防止对角拖动

ios - UIImage 中的颜色变换

ios - 具有现有仿射变换的 UIKit Dynamics

ios - 如何在不使用 Webview 的情况下在我的应用程序中播放 Youtube 视频

ios - 调整 UITableView 高度(嵌入在 UIScrollView 中)