【问题标题】:How do I add UIControls to a UIScrollView contentView?如何将 UIControls 添加到 UIScrollView contentView?
【发布时间】:2011-03-28 10:36:32
【问题描述】:

我想将 UIControls 添加到 UIScrollView,例如。

UIControl *couponView = [[UIControl alloc] initWithFrame:CGRectMake(drawX,drawY,couponWidth,couponHeight)];
couponView.backgroundColor = [UIColor redColor];    

[[scrollView contentView] addSubview:couponView]; //may not respond
    [scrollview.contentView addSubview:couponView]; //not a member

我还想根据我添加的 UIControls 的数量来设置 contentView 的框架。我想我会将框架设置在远离屏幕的 Y 方向上,并且 UIScrollView 会在用户滚动时将屏幕外部分滚动到可见性。

我在 UIScrollView 中没有看到 contentView 属性。我如何访问 contentView?

【问题讨论】:

  • 哇!我所要做的就是添加到 subView,之后似乎会自动处理事情:)

标签: iphone objective-c xcode uiscrollview uicontrol


【解决方案1】:

UIScrollView 与桌面版NSScrollView 在几个方面有所不同。

其中之一是它不提供contentViewdocumentView 访问器。相反,您可以将内容直接添加到 UIScrollView

只需将子视图添加到滚动视图并设置其边界(或contentSize)以包含所有子视图。

【讨论】:

    【解决方案2】:

    要向 uiscrollview 添加内容,您需要使用 addSubview 方法向其中添加子视图。您设置 scrollView 内容的宽度,它会在用户拖动时移动子视图。

    【讨论】:

      【解决方案3】:

      您必须自己添加该属性。只需使用 addSubview 将常规 UIView 添加到滚动视图即可。

      【讨论】:

        猜你喜欢
        • 2019-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 2019-12-28
        • 2015-06-29
        • 2017-12-19
        • 2023-03-28
        相关资源
        最近更新 更多