2012-11-05 78 views
0

這個事實背後的邏輯是什麼主屏幕和self.view始終保持相同的寬度和高度尺寸,而不管方向如何(即使self.view的尺寸正確,以適應當前的設備方向),而sub - 當設備方向改變時,self.view查看返回修改的寬度和高度尺寸?爲什麼self.view的高度和寬度與子視圖的高度和寬度不同?

NSLog(@"screen bounds w: %f, h: %f", [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height); 

NSLog(@"self.view frame w: %f, h: %f", self.view.frame.size.width, self.view.frame.size.height); 

NSLog(@"myView frame w: %f, h: %f", _myView.frame.size.width, _myView.frame.size.height); 

日誌輸出肖像

2012-11-05 16:15:11.152 test[2807:11303] screen bounds w: 320.000000, h: 480.000000 
2012-11-05 16:15:11.153 test[2807:11303] self.view frame w: 320.000000, h: 460.000000 
2012-11-05 16:15:11.153 test[2807:11303] myView frame w: 320.000000, h: 460.000000 

日誌輸出景觀

2012-11-05 16:14:38.800 test[2807:11303] screen bounds w: 320.000000, h: 480.000000 
2012-11-05 16:14:38.801 test[2807:11303] self.view frame w: 300.000000, h: 480.000000 
2012-11-05 16:14:38.801 test[2807:11303] myView frame w: 480.000000, h: 300.000000 

回答

1

其實比自動調整大小面具更多。所以當你改變設備的方向時,屏幕的物理特性保持不變。如果您將視圖設置爲自動旋轉,則會爲您翻譯寬度和高度。所以當要求翻譯視圖的屏幕尺寸時,你可以根據self.view.centerself.view.bounds而不是self.view.frame進行計算。

1

這取決於你的子視圖什麼autoSizingMask屬性集。

我推測你沒有使用iOS 6.0的AutoLayout功能。如果你這樣做,那麼你需要更多地瞭解iOS 6.0中的Constraints設置。

默認情況下,您的視圖的自動調整掩碼爲「Scale to Fill」,請參閱附加圖像。您可以根據您的要求爲您的子視圖設置相同的值。有關Apple文檔中有關Struts和Springs的更多信息,請參閱。

如果自動調整不適合您的要求,那麼您將需要實現自定義方法來對方向更改的視圖進行佈局。像下面的東西;

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [UIView animateWithDuration:duration animations:^{ 
     [self layoutForOrientation:toInterfaceOrientation]; 
    }]; 
} 

#pragma mark - Layout views 

- (void)layoutForOrientation:(UIInterfaceOrientation)orientation { 
    if (UIInterfaceOrientationIsPortrait(orientation)) 
     [self layoutPortraitViews]; 
    else 
     [self layoutLandscapViews]; 
} 

- (void)layoutLandscapViews { 
    // Layout your landscape views 
} 

- (void)layoutPortraitViews { 
    // Layout your portrait views 
} 

default autosizing mask for main view

1

[UIScreen mainScreen]對iPhone總是320w480h568h適用於iPhone5)。
尺寸爲[UIScreen mainScreen]正在拍攝縱向。

根據您的NSLog的設備狀態欄在您的應用中可見。由於這個原因,你在橫向上有300h,肖像中有460h