gpt4 book ai didi

iphone - 如何在 'for' 循环中添加 subview

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:44 26 4
gpt4 key购买 nike

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

最佳答案

试试这个:

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/

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