如何在ipad中找到最后的视图宽度

时间:2012-09-26 10:14:02

标签: ios4 uiview cgrectmake

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

  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。 有谁帮我?

1 个答案:

答案 0 :(得分: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];