【问题标题】:UIScrollView - content goes out bound when scrollingUIScrollView - 滚动时内容超出范围
【发布时间】:2013-05-28 21:21:53
【问题描述】:

我在界面生成器中这样设计滚动视图

这里看起来不错。但不幸的是,当我在模拟器或设备上运行它时

变成了

滚动视图中的内容在滚动视图本身之外展开,即使在包含此滚动视图的 UIView 之外。

在我的 viewDidLoad 中(面板是 scrollView 的容器)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat adjustPanelHeight = [PTTScreenScaleUtil getAdjustHeight:self.panel.frame.size.height];
    CGRect panelRect = self.panel.frame;
    panelRect.size.height = adjustPanelHeight;
    self.panel.frame = panelRect;

    UIImage *image = [UIImage imageNamed:@"panel-background"];
    UIGraphicsBeginImageContext(self.panel.frame.size);
    [image drawInRect:CGRectMake(0, 0, self.panel.frame.size.width, adjustPanelHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self.panel setBackgroundColor:[UIColor colorWithPatternImage:newImage]];

    NSLog(@"scrollView Height : %f", self.scrollView.frame.size.height);
    NSLog(@"scrollView contentSize Height : %f", self.scrollView.contentSize.height);
//    CGRect scrollViewRect = self.scrollView.frame;
//    CGRect scrollViewContentRect = self.scrollView.frame;
//    NSLog(@"ScrollView Height Before : %f , After : %f", self.scrollView.frame.size.height, [PTTScreenScaleUtil getAdjustHeight:self.scrollView.frame.size.height]);
//    scrollViewRect.size.width = 280;
//    scrollViewRect.size.height = [PTTScreenScaleUtil getAdjustHeight:270];
//    self.scrollView.frame = scrollViewRect;
//    [self.detailsLabel sizeToFit];
    UIView *view = [[self.scrollView subviews] objectAtIndex:0];
//    [view sizeToFit];
//    [self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
//    NSLog(@"ContentSize Height : %f", view.frame.size.height);
//    scrollViewContentRect.size.height = view.frame.size.height;
    NSLog(@"Bounds : %f", view.bounds.size.height);
    self.scrollView.frame = CGRectMake(10, 10, 280, 270);
    self.scrollView.contentSize = CGSizeMake(280, 500);
    NSLog(@"Frame Height %f", self.scrollView.frame.size.height);
    //[self.scrollView setContentSize: CGSizeMake(280, 1000)];

    CGRect termBtnRect = self.termBtn.frame;
    CGRect mailBtnRect = self.mailBtn.frame;
    CGRect twitterBtnRect = self.twitterBtn.frame;
    CGRect fbBtnRect = self.fbBtn.frame;
    termBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    mailBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    twitterBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    fbBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
    self.termBtn.frame = termBtnRect;
    self.mailBtn.frame = mailBtnRect;
    self.twitterBtn.frame = twitterBtnRect;
    self.fbBtn.frame = fbBtnRect;
}

所有日志返回 270.0

PS。即使内容在外面,滚动条也是正确的,但滚动条工作正常(留在滚动视图的框架中,如界面构建器中的排列) 我不知道我该如何解决这个问题。 请任何人帮助我。

谢谢。

【问题讨论】:

  • 您是否在代码中的其他任何地方更改滚动视图的框架?
  • scrollView 大小没有变化(这一行 self.scrollView.frame = CGRectMake(10, 10, 280, 270); 我只是给出与在 Size Inspector 中设置 scrollView 相同的值)。所有这些adjustHeight 都是针对iphone5 的。 iphone5以下,返回相同的值。
  • 尝试以编程方式创建和添加一个滚动视图,看看是否会遇到同样的问题。你知道怎么做吗?

标签: ios objective-c uiscrollview


【解决方案1】:

通过在界面生成器中创建新的视图控制器来解决这个问题,并小心翼翼地重做相同的过程。它有效。

当我比较两个视图控制器时,我意识到错误的 UIScrollView Clip Subviews 未被选中。检查后问题就解决了。

【讨论】:

  • 显然在 UISCrollView 上设置剪辑子视图就可以了。感谢您的回答。
  • 将剪辑设置为边界也对我有用,但由于某种原因,在 IB 中设置时设置没有坚持。在代码中设置 clipsToBounds = true 有帮助
【解决方案2】:

我只是为此挣扎了一个小时,然后头晕目眩。

就我而言,我在情节提要的场景中有一个UIView。在某些时候,我决定我需要将它改为UIScrollView(与将UIScrollView 嵌入UIView 的原始计划相反)

我继续将UIView 上的课程更改为UIScrollView。 IB在Document Outline里改成Scroll View,我觉得挺好的吧?

然后我看到了你描述的行为。

有时我觉得这还不够。显然,通过 IB 添加UIScrollView 与添加UIView 有一些不同之处,仅仅改变类是不够的。这可能是从头开始为您修复它的原因。

因此,对于将来遇到此问题的任何人,请确保您通过 IB 添加了 UIScrollView 而不是 UIView

【讨论】:

    【解决方案3】:

    我遇到了与这篇文章中描述的相同的问题。我尝试了多种无效的解决方案组合,包括:

    • 将滚动视图放入 Clip To Bounds = YES 的视图中
    • 在 Clip To Bounds = YES 的滚动视图中放置一个视图,然后包含我的子视图
    • 在滚动视图中放置一个容器视图,然后嵌入我的子视图
    • 完全重建 Interface Builder 文件
    • 为滚动视图和容器视图系统地自动调整掩码选项的每个组合
    • 系统地为每个元素启用或禁用剪辑到边界

    有问题的子视图以前在滚动视图中工作,但在内容超出滚动视图边界的这种情况下不会。

    最后,我在代码中实现了解决方案,因为我找不到让 Interface Builder 合作的方法:

    // There are two scroll areas on the screen, the left view and the right view.
    // We want the right view to contain a scrollable area with another child view controller
    // we designed in Interface Builder.
    
    // create a scroll view to fill the right view with a scrollable area
    CGSize rightFrameSize = self.rightView.bounds.size;
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0, 0, rightFrameSize.width, rightFrameSize.height)];
    scrollView.contentSize = CGSizeMake(640, 1352);
    [self.rightView addSubview:scrollView];
    
    // now create our child view controller from Interface Builder and add it to the scroll view
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CustomerAddress" bundle:[NSBundle mainBundle]];
    detailsView = [sb instantiateViewControllerWithIdentifier:@"CustomerDetailsView"];
    detailsView.delegate = self;
    detailsView.customer = _customer;
    [scrollView addSubview:detailsView.view];
    

    您当然可以使用 scrollView.contentSize = detailsView.view.frame.size 从您在 Interface Builder 中构建的子视图控制器中获取 scrollView.contentSize。

    我对 Interface Builder 的爱恨交织……大多数时候我都喜欢它,但有时我们会争吵,但我希望我们从未见过面……:)

    【讨论】:

      【解决方案4】:

      如果您将 UIView 更改为 UIScrollView,则仅在情节提要中设置 cliptobound=YES

      【讨论】:

        猜你喜欢
        • 2020-01-08
        • 2010-11-27
        • 1970-01-01
        • 2018-05-03
        • 2020-04-21
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        相关资源
        最近更新 更多