2

我在InterfaceBuilder的UIViewController中有一個自定義的UIView videoView更改方向時定位UIview

當我加載它時,它看起來很好,但是當我把它說成「從肖像風景」它不會去座標告訴它的地方。然後當我把它回來時,它又錯了。

我有以下代碼:

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self processRotation:self.interfaceOrientation]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
}  
-(void)processRotation:(UIInterfaceOrientation)_interfaceOrientation 
{ 
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeLeft || _interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f); 
    } 

else if (_interfaceOrientation == UIInterfaceOrientationPortrait || _interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     videoView.frame = CGRectMake(20.0f, 297.0f, 728.0f, 453.0f); 
    } 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self processRotation:toInterfaceOrientation]; 
} 
+0

嗨代代,我可以推薦一個最近的帖子,它會幫助你的查詢:http://stackoverflow.com/questions/6580052/how-to-change-view-of-the-viewcontroller-when-改變方向在iPhone/6580111#6580111 – Luke

回答

5

當視圖旋轉時,確保你從縱向將景觀:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation) 

由於該方位還沒有發生變化,而不是,

videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f); 

寫:

videoView.frame = CGRectMake(77.0f, 22.0f, 595.0f, 984.0f); 

或者,您可以進入Interface Builder並在其中更改自動調整掩碼,並且不要在代碼中執行任何操作。要更改自動調整掩碼,請轉到右窗格中的「大小」檢查器(標尺圖標)。你會看到一個正方形,裏面有一個垂直和水平的箭頭,並且一個字母形狀的線條從每邊出來。當紅色(打開,點擊打開)時,內部的箭頭會在視圖大小調整時自動沿該方向拉伸。打開一條I形線基本上將視線停靠在該邊上,這意味着視圖邊和該邊之間的距離將始終保持不變。 例如如果您的頂部有一個工具欄,它的橫向變薄,請選擇而不是,否則您的視圖最終會離頂部工具欄幾英里遠。

或者你也可以不返回的自轉和實現它自己,通過調用

self.interfaceOrientation = toInterfaceOrientation; 

,然後手動更改像座標在原始代碼。

你真的需要嘗試一下,我一直遇到這個問題,每次修復它都會有所不同。

+0

嘿,你還可以提供更多的信息在自動調整大小的面具作爲參考。 +1 –

0

您使用willRotateToInterfaceOrientation:,旋轉發生之前被調用。如果videoView具有autoresizingMask設置,那麼稍後會自動調整大小,並更改視圖的位置。

你通常應該在- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration做這一點,並保證,您videoView的autoresizingMask設置爲UIViewAutoresizingNone