2012-01-31 26 views
0

到App開始我想,這可能已經問過,但讀了很多之後,我不知道我已經找到了答案。的iOS:調整大小和定位新的視圖中添加的景觀

當應用程序被旋轉爲橫向,我添加到主視圖的新視圖。新視圖被構建和加入這樣的代碼:

UIView * newView = .... 
[rootView addSubview:newView]; 

但因爲當我添加新視圖模擬器旋轉爲橫向,我得到這樣的:

+------------------------------------------------------+ 
+ +---------------------------------------------+  + 
+ |       |     |  + 
+ |       |     |  + 
+ |  newView    | rootView  |  + 
+ |  (Landscape   | (Landscape) |  + 
+ | but portrait size)  |     | () + 
+ |       |     |  + 
+ |       |     |  + 
+ |       |     |  + 
+ |       |     |  + 
+ +---------------------------------------------+  + 
+------------------------------------------------------+ 

所以我添加newView.bounds = rootView.bounds; newView.center = rootView.center;思想,將定位NewView的直接在rootView的頂部,而是我得到這個:

+------------------------------------------------------+ 
+ +---------------------------------------------+  + 
+ |            |  + 
+ |            |  + 
+ |--------------------------+ rootView  |  + 
+ |       | (Landscape) |  + 
+ |   newView   |     | () + 
+ | (Landscape size,  |     |  + 
+ | but offset)   |     |  + 
+ |       |     |  + 
+ |       |     |  + 
+ +---------------------------------------------+  + 
+------------------------------------------------------+ 

我試圖找出爲什麼它們是不同的,即使我已經所設定的範圍和中心是相同的。

傾出我得到這個觀點:

rootView; frame = (0 0; 768 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x98870a0>> 
newView ; frame = (-128 128; 1024 768); autoresize = W+H; layer = <CALayer: 0x6e470a0>> 

所以看來rootView仍然768瓦特X 1024H,只是變換旋轉,並設置範圍和中心後,新的觀點是寬1024 x長768h,並通過128點的偏移量。

回答

0

經過一些玩弄,我想出了這個:

// Position the new view offscreen. 
CGFloat width = rootView.bounds.size.width; 
CGFloat height = rootView.bounds.size.height; 
newView.frame = CGRectMake(0, height, width, height); 
[rootView addSubview:newView]; 

// Animate to the center of the view. 
[UIView animateWithDuration:1.0 animations:^{ 
    newView.frame = CGRectMake(0,0, width, height); 
}]; 

它的工作原理,我只是想知道是否有更好的解決方案。

相關問題