2012-03-04 71 views
2

改變方向改變UI的最好方法是什麼?iOS改變方向改變UI的最佳方式

例如,使用兩個不同的UIView一個人像和一個風景,並顯示其中一個方向是否改變,或者使用一個UIView並更改UI控件的大小和位置?

還有其他想法嗎?

回答

2

我總是建議在視圖控制器視圖中爲所有子視圖使用autoresizingmask。在正確設置的情況下,所有視圖都將從方向自動調整大小,並且不需要需要額外的旋轉特定子視圖(一個縱向視圖和一個橫向視圖)。

+0

你好 某些情況下不給autoresizingmask,例如存在於縱向視圖9x9的UI按鈕預期的效果,我怎麼可以用建議的方法做到這一點?如果我將視圖更改爲橫向,則需要水平移動某些按鈕,因爲不在可見區域。 – Laszlo 2012-03-04 10:15:06

+0

這並不意味着您希望將視圖邊緣的邊距保留在按鈕上嗎?如果是這種情況,那麼你可以通過autoresizingmask來實現。 – 2012-03-05 16:06:34

+0

好的,我怎樣才能用計算器按鈕做這個例子? – Laszlo 2012-03-26 08:51:46

5
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
     toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     NSLog(@"Change to custom UI for landscape"); 
    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
     toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"Change to custom UI for portrait"); 

    } 
} 
+0

請注意'''''willAnimateRotationToInterfaceOrientation'''現已被棄用(參見[doc](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621458-willanimaterotationtointerfaceor))。 – Frouo 2017-11-20 10:06:09