【问题标题】:How to set background color to the content area in UIScrollView?如何为 UIScrollView 中的内容区域设置背景颜色?
【发布时间】:2012-11-21 21:23:30
【问题描述】:

我在我的应用程序中使用UIScrollView,我的要求是我必须为UIScrollView 添加背景颜色,但是当用户在最顶部或最底部拖动内容/外侧以外的区域时滚动视图中显示的内容应该以不同的颜色显示,就像在UIWebView 中一样。在网络视图中,如果我们在顶部向下拖动或在底部向上拖动,它将显示默认背景图像/灰色,并且网络视图内容背景颜色是差异(默认白色)。任何人都可以帮助我实现这一点。

谢谢。

【问题讨论】:

    标签: iphone ios ipad uiscrollview uiscrollviewdelegate


    【解决方案1】:

    为内容添加一个视图,并将滚动视图的颜色设置为与保存内容的视图的颜色不同的颜色。

    【讨论】:

      【解决方案2】:

      诀窍是将子视图添加到位于内容上方和下方的 ScrollView。这样,它们只会在内容被向上或向下拖动时出现。

      UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
      
      UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, -200, scroll.bounds.size.width, 200)];
      [topView setBackgroundColor:[UIColor darkGrayColor]];
      [scroll addSubview:topView];
      [topView release];
      
      UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, scroll.bounds.size.height, scroll.bounds.size.width, 200)];
      [bottomView setBackgroundColor:[UIColor darkGrayColor]];
      [scroll addSubview:bottomView];
      [bottomView release];
      

      【讨论】:

      • 我认为对于bottomView的frame,y值应该是:scroll.contentSize.height
      【解决方案3】:

      只需设置背景颜色。

      [scrollview setBackgroundColor:[UIColor whiteColor]];

      【讨论】:

        猜你喜欢
        • 2014-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多