2013-03-02 23 views
1

我在想如何/如果我可以做到這一點:在superview中居中一組UIView的。最終的結果將是一組「視圖」,它們明顯地集中在他們的超級視圖中。iOS編程中心組UIViews

回答

1

那麼,這是一個2步過程;首先將UIViews添加到一個數組,然後執行快速列舉它們中心作爲「基團」

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

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

self.label3= [[UILabel alloc]init]; 
self.label3.font=[UIFont systemFontOfSize:30.0]; 
[email protected]"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和α設置爲只是在這裏測試目的。

希望這會有所幫助。

+0

'initWithCapacity:4'的用途是什麼? – Undo 2013-03-02 05:01:14

+0

在使用它之前,您需要先分配init NSMutableArray。可變數組根據需要展開; 4簡單地確定了對象的初始能力。如果你願意投入2,那也可以。 – Spectravideo328 2013-03-02 12:39:15

+0

好的,你可以*只使用'init'? – Undo 2013-03-02 16:22:45