gpt4 book ai didi

ios - UIScrollView 不滚动 iOS

转载 作者:行者123 更新时间:2023-11-28 20:09:57 25 4
gpt4 key购买 nike

我正在尝试在 iOS 中水平滚动的单个 View 中创建多个 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);

但是,没有一个 ScrollView 实际上在滚动,我对此感到困惑,因为添加了多个按钮。另外,我添加的按钮在我按下它们时实际上没有做任何事情。他们应该运行 buttonPressed 方法,但实际上没有?

如有任何帮助,我们将不胜感激!

最佳答案

试试这个,

float xPosition = 0;
float xPosBut = 0;

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];

scrollView.backgroundColor = [UIColor yellowColor];

for (int i = 0; i < 10; i++) {
UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:@"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosition += testButton.frame.size.width;
NSLog(@"xposbut = %f", xPosBut);
NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width);

xPosition += scrollView.frame.size.width+2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition +=scrollView.frame.size.width+ 4;

[self.view addSubview:scrollView];
[scrollView addSubview:seperatorView];
[scrollView addSubview:testButton];
[builder addObject:testButton];

}
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
[self.view addSubview:scrollView];

根据您的要求设置 for 循环条件部分。希望这对您有所帮助。

关于ios - UIScrollView 不滚动 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20679766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com