我在主视图中添加视图。如何找到最后插入的视图宽度?
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。 有谁帮我?
答案 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];