【发布时间】:2013-04-08 17:51:34
【问题描述】:
我有一个问题,如何在 UIView 中添加 UIScrollView,以便 UIScrollView 可以正常工作。
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * container.frame.size.width/2;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];
以上代码来自教程:http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/ 但这对我不起作用。
编辑:
如果您在正确设置滚动视图但无法正常工作时遇到问题,请确保您的滚动视图没有与另一个 UIView 重叠。那是我的问题。 解决了!
【问题讨论】:
-
你看不到红色滚动视图吗?
-
我看到第一个 UIView 屏幕 colorWithRed:0.5/i.
-
猜猜,你的 awesomeView 比滚动视图本身的宽度和高度更大。这意味着滚动视图正在被添加,尝试增加滚动视图的框架和内容大小。
-
我认为您的 xOrigin 计算错误,awesomeView 的,必须重叠
-
我可以看到 UIViews。我不能只滚动 UIScrollView。
标签: objective-c uiview uiscrollview