You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置ScrollView使其内容不与底部GUI/Banner重叠?

解决ScrollView内容滑入底部Banner的问题

方案一:调整ScrollView的Frame(推荐)

问题根源是你的ScrollView占据了整个屏幕,导致内容会滚入底部Banner区域。最直接的解决方式是缩小ScrollView的高度,让它的底部刚好停在Banner的顶部。

  1. 先确定底部Banner的实际高度(比如设为bannerHeight),然后修改ScrollView的frame:
CGFloat bannerHeight = 60; // 替换成你项目中Banner的真实高度
CGRect scrollFrame = scroll.frame;
scrollFrame.size.height = self.view.bounds.size.height - bannerHeight;
scroll.frame = scrollFrame;
  1. 优化contentSize的计算(避免硬编码):
    你当前代码里硬写了1760和3240,建议根据按钮的排列动态计算内容高度,既准确又适配不同屏幕:
- (void)reloadScroll
{
    for(UIView *view in [scroll subviews])
        [view removeFromSuperview];
    
    CGFloat bannerHeight = 60; // 替换为实际Banner高度
    // 调整ScrollView范围,避开底部Banner
    CGRect scrollFrame = scroll.frame;
    scrollFrame.size.height = self.view.bounds.size.height - bannerHeight;
    scroll.frame = scrollFrame;
    
    CGFloat contentHeight;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        contentHeight = 20 + 10 * (300 + 20); // 10行:顶部边距20 + 每行(按钮高度300+间距20)
        for(int i = 0; i < 20; i++)
        {
            int xCoord = (i % 2 == 0) ? 156 : 462;
            UIButton *glasss = [[UIButton alloc] initWithFrame:CGRectMake(xCoord, 20 + (i/2)*(300+20), 150, 300)];
            // 这里添加按钮的配置逻辑(图片、点击事件等)
            [scroll addSubview:glasss];
        }
    }
    else
    {
        contentHeight = 20 + 10 * (150 + 20); // 10行:顶部边距20 + 每行(按钮高度150+间距20)
        for(int i = 0; i < 20; i++)
        {
            int xCoord = (i % 2 == 0) ? 57 : 188;
            UIButton *glasss = [[UIButton alloc] initWithFrame:CGRectMake(xCoord, 20 + (i/2)*(150+20), 75, 150)];
            // 这里添加按钮的配置逻辑(图片、点击事件等)
            [scroll addSubview:glasss];
        }
    }
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, contentHeight);
}

方案二:设置ContentInset(适合ScrollView背景需覆盖Banner的场景)

如果需要ScrollView的背景延伸到Banner下方,但内容不能重叠,可以通过设置contentInset让内容底部留出Banner高度的空白:

CGFloat bannerHeight = 60; // 替换为实际Banner高度
scroll.contentInset = UIEdgeInsetsMake(0, 0, bannerHeight, 0);
scroll.scrollIndicatorInsets = scroll.contentInset; // 保持滚动指示器和内容同步

这种方式下ScrollView仍占满屏幕,但内容滚动到底部时会自动停在Banner上方,不会重叠。


内容的提问来源于stack exchange,提问作者DannyRoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 03:30:46