我想在iOS中水平滾動的單個視圖中創建多個UIScrollViews。這是到目前爲止我的代碼:UIScrollView不滾動iOS
-(void)updateSection {
[feedLoadingActInd stopAnimating];
feedLoadingActInd.hidden = YES;
builder = [NSMutableArray array];
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
for (int i = 0; i < itemArticleArray.count; i++) {
testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:@"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosBut += testButton.frame.size.width;
NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += 2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition += 350;
[seperatorView addSubview:testButton];
[scrollView addSubview:seperatorView];
[builder addObject:testButton];
}
[self addSubview:scrollView];
[scrollView setUserInteractionEnabled:YES];
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
NSLog(@"scroll.contentsize.width = %f", scrollView.contentSize.width);
}
然而,沒有任何的滾動意見實際上是滾動的,我感到困惑,因爲有添加多個按鈕。另外,我添加的按鈕在按下按鈕時並沒有做任何事情。他們應該運行buttonPressed方法,而不是?
任何幫助將不勝感激!
您正在將滾動視圖內容大小設置爲等於框架,因此組件不會滾動。爲了滾動,scroll.contentSize.y> scroll.frame.height –
[scrollView setContentSize:CGSizeMake(xPosition,700)];或者你通過生成的按鈕高度調整高度 –
犯了一個小錯誤,我需要它水平滾動不垂直 –