iphone - 如何在 'for' 循环中添加 subview
<p><p>我只是想为数组中的每个对象添加一个 UIView,而不在屏幕上显示超过 3 个,但 View 并不相邻。每个 View 之间有一个很大的间隙(一个 View 宽度)。这就是我所拥有的;</p>
<pre><code>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);
;
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [ initFrame:r];
;
}
</code></pre>
<p> <img src="/image/kajnA.png" alt="The Orange is the scrollView underneath"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个:</p>
<pre><code>int xPosition = 0;
for (int i = 0; i < users.count; i++) {
UIView * view = [ initFrame:CGRectMake(xPosition, 0, width, 1200)];
;
xPosition += width;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 如何在'for' 循环中添加 subview ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9183973/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9183973/
</a>
</p>
页:
[1]