【发布时间】:2013-12-19 10:54:03
【问题描述】:
我正在尝试在 iOS 中水平滚动的单个视图中创建多个 UIScrollView。到目前为止,这是我的代码:
-(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)];或者你通过生成的按钮高度来调整你的高度
-
犯了一个小错误,我需要它水平滚动而不是垂直滚动
-
我已经编辑了上面的帖子和代码?
标签: ios uiscrollview