[英]Get launch orientation of iPad app
在我的iPad應用程序中,我需要運行一些布局代碼,以根據方向設置正確的布局。 默認情況下,布局是針對橫向方向配置的,因此在應用程序以縱向模式啟動的情況下,我需要采取額外操作來正確配置視圖以便以縱向顯示。
在我的-application:didFinishLaunchingWithOptions:
方法中,我使用[[UIDevice currentDevice] orientation]
檢查[[UIDevice currentDevice] orientation]
。 這里的問題是,即使應用程序以橫向方式啟動,它也始終返回縱向。 有沒有辦法解決?
這是預期的行為。 請參閱UIViewController文檔 :
注意:在啟動時,應用程序應始終以縱向方式設置其界面。 在
application:didFinishLaunchingWithOptions:
方法返回后,應用程序使用上述視圖控制器旋轉機制在顯示窗口之前將視圖旋轉到適當的方向。
換句話說,就設備而言,在應用程序啟動時,方向是縱向。 在application:didFinishLaunchingWithOptions:
之后的某個時刻application:didFinishLaunchingWithOptions:
它將檢測不同的方向並調用你的shouldAutorotateToInterfaceOrientation:
方法,然后調用你應該正常處理的其他視圖旋轉方法 。
這是檢查發布方向的最佳方法。 首先,在AppDelegate中創建一個檢查方向的新方法:
-(void)checkLaunchOrientation:(id)sender{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL isLandscape = UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation);
if (UIInterfaceOrientationIsLandscape(orientation) || isLandscape) {
//do stuff here
}
}
在-application:didFinishLaunchingWithOptions:
結束時-application:didFinishLaunchingWithOptions:
運行
[self performSelectorOnMainThread:@selector(checkLaunchOrientation:) withObject:nil waitUntilDone:NO];
在視圖控制器中使用self.interfaceOrientation
- 它是UIViewController
的一個屬性,由iOS為您設置,在某些情況下比[[UIDevice currentDevice] orientation]
更可靠。
以下是詳細說明: http : //bynomial.com/blog/?p = 25
正如上面的博客文章中所提到的,有一組用於測試方向的宏。 然而,該博客文章提到了UIDeviceOrientationIsPortrait。 我喜歡下面的內容,這是一個小小的轉折。
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
NSLog(@"Portrait");
}
else
{
NSLog(@"Landscape");
}
我做的一個觀察是你不能在表視圖中調用這個代碼,推到嵌入在拆分視圖控制器中的導航控制器。 換句話說,你不能從主視圖控制器調用它。 假設您維護對父拆分視圖控制器的引用,則必須使用splitviewcontroller.interfaceOrientation替換“self.interfaceOrientation”。
而是使用狀態欄方向來檢測它。
UIInterfaceOrientation orientation = [UIApplication sharedApplication] .statusBarOrientation;
然后在你從上面獲得的“方向”上執行if。
所以問題是關於在啟動時檢查方向。 答案很遺憾“你不能”。
但啟動后 ,你可以檢查方向正常方式(如其他人所描述的)。
如果有其他人來這里尋找答案,只需停止查看,因為在啟動時未設置方向變量(所有視圖框架/邊界也報告為縱向,即使它們不是)。
你無法弄清楚發射方向是不正確的,這樣做確實是后方的痛苦。
這是你需要做的。
你的第一個UIViewController需要一些特殊的邏輯來獲取你想要的信息。
你可能甚至想為這些目的創建一個UIStartupController,如果它對你的流程很重要的話。
在我的項目中,我們已經有了這樣的啟動控制器。
您只需要以下代碼即可
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.launchOrientation = UIDeviceOrientationUnknown;
}
return self;
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (self.launchOrientation == UIDeviceOrientationUnknown && duration > 0)
self.launchOrientation = UIInterfaceOrientationPortrait;
else
self.launchOrientation = toInterfaceOrientation;
}
基本上,如果我們沒有在UIInterfaceOrientationPortrait中啟動,第一個旋轉回調序列將實際顯示啟動方向。
如果在UIInterfaceOrientationPortrait中啟動,那么我們需要檢查第一個旋轉的持續時間是否為非零,然后我們知道它是從肖像啟動的。
您需要確保在Info.plist中設置正確的密鑰以允許所需的方向:
UISupportedInterfaceOrientations
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
並不是說你需要另一個答案,但我想我應該補充說你幾乎不想使用[[UIDevice currentDevice] orientation]
。 該方法返回設備的方向,該方向不一定與接口的方向相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.