如何将 UIControl 添加到 UIScrollView contentView?
我想将 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
我还想根据我添加的 UIControl 数量设置 contentView 的框架。我想我会将框架设置在远离屏幕的 Y 方向上,并且每当用户滚动时 UIScrollView 都会将屏幕外的部分滚动到可见状态。
我在 UIScrollView 中没有看到 contentView 属性。如何访问 contentView?
I want to add UIControls to a UIScrollView, eg.
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
I also want to set the frame of the contentView based on how many UIControls I'm adding. I'm thinking I'll set the frame far in the Y direction off screen, and the UIScrollView will scroll the off-screen parts into visibility whenever the user scrolls.
I didn't see a contentView property in UIScrollView.. how can I access the contentView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIScrollView
与桌面版本NSScrollView
有几个方面不同。其中之一是它不提供
contentView
或documentView
访问器。相反,您可以直接将内容添加到UIScrollView
。只需将子视图添加到滚动视图并设置其边界(或
contentSize
)即可包含所有子视图。UIScrollView
differs from the desktop versionNSScrollView
in a couple of ways.One of them is that it does not provide
contentView
ordocumentView
accessors. Instead, you'd add content directly to theUIScrollView
.Just add subviews to the scroll view and set its bounds (or
contentSize
) to contain all the subviews.要将内容添加到 uiscrollview,您需要使用 addSubview 方法向其添加子视图。您设置滚动视图内容的宽度,它将随着用户拖动而移动子视图。
To add content to a uiscrollview, you need to add subviews to it, with the addSubview method. You set the width of the scrollView content and it will move the subviews as the user drags.
您必须自己添加该属性。只需使用 addSubview 将常规 UIView 添加到滚动视图即可。
You'd have to add that property yourself. Just add a regular UIView to the scrollview with addSubview.