我正在爲iOS編寫一個UI,我需要創建一個筆記類型小部件的滾動視圖。我正在嘗試創建一個scrollview並添加一些UIView對象,但由於某種原因,我無法滾動,我是否做錯了什麼?下面是我在做什麼:添加多個項目到UIScrollView,但不能滾動?
[_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;
}
我會一直期待通過滾動視圖滾動,並能夠通過所有的100個UIViews的補充。我錯過了什麼?
您需要將滾動視圖的內容大小設置爲所有uiviews的高度。在循環之後使用setContentSize:cgSizeMake(_testScrollView.frame.size.width,size * height) – Bergasms
您的先生是天才,就是這樣!謝謝! – easythrees
@Bergasms你應該添加你的評論作爲答案。 – danielbeard