OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何在 'for' 循环中添加 subview [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:20
标题: iphone - 如何在 'for' 循环中添加 subview

我只是想为数组中的每个对象添加一个 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];
}    

The Orange is the scrollView underneath



Best Answer-推荐答案


试试这个:

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4