iOS编程中心UIViews组

时间:2013-03-02 00:34:44

标签: ios view uiview geometry centering

我想知道如何/如果我能做到这一点:将一组UIView置于超级视图中。最终结果将是一组明显以其超级视图为中心的“组”。< / p>

1 个答案:

答案 0 :(得分:1)

嗯,这是一个两步过程;首先将UIViews添加到数组中,然后执行快速枚举以将它们作为“组”中心

//step 1: create the objects
self.label1= [[UILabel alloc]init];
self.label1.font=[UIFont systemFontOfSize:30.0];
self.label1.text=@"1";

self.label2= [[UILabel alloc]init];
self.label2.font=[UIFont systemFontOfSize:30.0];
self.label2.text=@"2";

self.label3= [[UILabel alloc]init];
self.label3.font=[UIFont systemFontOfSize:30.0];
self.label3.text=@"3";


//step2: create the array and add the objects to the array
self.arrayOfViews=[[NSMutableArray alloc]initWithCapacity:4];
[self.arrayOfViews addObject:self.label1];
[self.arrayOfViews addObject:self.label2];
[self.arrayOfViews addObject:self.label3];



//step3: use fast enumeration to be able to control them as "group"
for(UIView* currentViewObject in self.arrayOfViews)
{
    currentViewObject.alpha=0.3;
    currentViewObject.frame=CGRectMake(self.view.bounds.size.width/2-sizeOfObject/2,self.view.bounds.size.height/2-sizeOfObject/2,sizeOfObject,sizeOfObject);
    [self.view addSubview:currentViewObject];
}

步骤1和alpha设置仅用于测试目的。

希望这有帮助。