我只是想为数组中的每个对象添加一个 UIView,而不在屏幕上显示超过 3 个,但 View 并不相邻。每个 View 之间有一个很大的间隙(一个 View 宽度)。这就是我所拥有的;
int numberOfUsersOnScreen;
if (array.count < 3) {
numberOfViewsOnScreen = array.count;
}else{
numberOfUsersOnScreen = 3;
}
double width = (self.scrollView.frame.size.width/numberOfViewsOnScreen);
CGRect r = CGRectMake(0, 0, width, 1200);
[self.usersScrollView setContentSize:CGSizeMake(width*array.count, 0)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
Best Answer-推荐答案 strong>
试试这个:
int xPosition = 0;
for (int i = 0; i < users.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, 0, width, 1200)];
[self.scrollView addSubview:view];
xPosition += width;
}
关于iphone - 如何在 'for' 循环中添加 subview ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/9183973/
|