1

I'm writing a UI for iOS and I need to create a scrolling view of note-type widgets. I'm experimenting with creating a scrollview and adding some UIView objects to it, but for some reason, I can't scroll, am I doing something wrong? Here's what I'm doing:

[_testScrollView setBackgroundColor:[UIColor purpleColor]];

NSUInteger size = 100;
float height = _testScrollView.frame.size.height/10;
float width = _testScrollView.frame.size.width;
float currTop = 0;

CGSize fooSize = CGSizeMake(_testScrollView.frame.size.width, _testScrollView.frame.size.height);
[_testScrollView setContentSize:fooSize];

for (NSUInteger i = 0; i < size; i++)
{
    double red = ((double)arc4random() / ARC4RANDOM_MAX);
    double green = ((double)arc4random() / ARC4RANDOM_MAX);
    double blue = ((double)arc4random() / ARC4RANDOM_MAX);

    CGRect currFrame = CGRectMake(0, currTop, width, height);
    UIView* testView = [[UIView alloc] initWithFrame:currFrame];
    [testView setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1.0]];
    [_testScrollView addSubview:testView];
    currTop += height;
}

I would've expected to scroll through the scrollview and be able to go through all of the 100 UIViews added. What am I missing?

4

2 回答 2

0

您需要将滚动视图的内容大小设置为所有 uiview 的高度。利用

setContentSize:cgSizeMake(_testScrollView.frame.size.width,size*height) 

在for循环之后

于 2013-08-22T03:24:50.323 回答
0

只需在滚动视图中打印(日志屏幕)您的内容大小的高度。在那里您可以看到滚动视图的高度...您需要添加所有子视图的高度以获得滚动视图的内容大小的高度...

于 2013-08-22T08:23:16.087 回答