0

我在主视图中添加视图。如何找到最后插入的视图宽度?

  NSArray *noPotions = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
int list = [noPotions count];

int lastPosition = 10;

int widthValue = 100;

for (int i = 0; i < list ; i++) {
    UIView  *newView = [[UIView alloc]initWithFrame:CGRectMake(lastPosition, 10, widthValue, 60)];
    [newView.layer setBorderColor:[[UIColor blueColor] CGColor]];
    [self.view addSubview:newView];
    lastPosition = newView.bounds.size.width + 20;
    newView.layer.borderWidth = 4;

    [newView release];

在这段代码的最后一个位置总是得到 120。有人请帮帮我吗?

4

1 回答 1

0

你总是可以先在 FOR 循环中给 UIViews 标签。

for (int i = 0; i < list ; i++) 
{
     UIView  *newView = [[UIView alloc]initWithFrame:CGRectMake(lastPosition, 10, widthValue, 60)];
     newView.tag = i;
     [newView.layer setBorderColor:[[UIColor blueColor] CGColor]];
     [self.view addSubview:newView];
     lastPosition = newView.bounds.size.width + 20;
     newView.layer.borderWidth = 4;

     [newView release];
}

稍后,要访问任何视图,您总是可以从他们的标签中获取它。

假设我们想要一个位于数组最后一个元素的视图......

UIView* latestView = [self.view viewWithTag:[list count]-1];
于 2012-09-26T10:36:34.503 回答