我有一個iPad應用程序。在橫向方向上,UIViewController
的視圖實際寬度= 1024px
和height = 768-20(statusBar) - 44(navigationBar)= 704px
。獲取UIViewController視圖的正確範圍
所以我想得到這[1024 x 704]
大小,我使用self.view.bounds
它。它返回[748 x 1024]
,這是錯誤的!但是當我旋轉屏幕兩次(當前 - >縱向 - >當前)時,視圖的邊界是正確的 - [1024 x 704]
。
有人初始化是這樣的:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor lightGrayColor];
}
和範圍都得到這樣的:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"bounds = %@", NSStringFromCGRect(self.view.bounds));
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"bounds = %@", NSStringFromCGRect(self.view.bounds));
}
所以,問題是..我怎樣才能得到正確的觀點在一開始綁定?
如果你正在創建大視圖,可能會延遲視圖外觀,你可以通過使用'viewWillAppear'減少延遲 – 2014-08-09 08:07:26
我試過viewWillAppear,它不能按要求工作,看來邊界計算是在視圖出現之後完成的 – Billy 2016-05-07 18:17:22
我強烈建議**不要**使用'viewDidAppear '你的視圖在屏幕上,用戶可以看到這一點,你做的改變會引起突然和可見的毛刺,它也不會幫助你響應旋轉期間視圖大小的變化(等等)。 'viewWillLayoutSubviews'](http://stackoverflow.com/a/42275589/2547229)(披露:鏈接到我的答案)這是iOS處理這個的方式。 – Benjohn 2017-02-16 13:52:57