2011-12-16 79 views
2

我有一個支持所有設備方向的iPhone應用程序,但我有一個特定的模態視圖,我只想在橫向上顯示。顯示模態視圖,其方向不同於設備的方向

如果設備處於橫向方向,它可以正常工作,但如果設備在縱向方向上保持不變,則不會顯示我想要的方式。

由於設備以縱向方向保持,所以圖像被裁剪並且左側和右側離開顯示器的邊緣。由於圖像的高度僅與狹窄的尺寸一樣高,因此頂部和底部顯示父視圖的背景。如果我然後旋轉設備到橫向方向,那麼一切都很好。

在我的研究,我經常遇到shouldAutorotateToInterfaceOrientation,但我相信這是當設備在物理上旋轉,只有被炒魷魚這並不適用於我的情況的事件。該設備在物理上保持縱向,但我想顯示該視圖,就好像它在橫向上一樣。

[UIApplication的sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]也提到但我從來沒有看到有什麼影響時,我一直在使用它嘗試。

所以我想我的問題是,如果有一種簡單的方法來強制視圖的初始顯示方向不同於設備的物理方向,以及如何執行此操作。

我使用下面的邏輯來模態地顯示我的觀點:

TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]]; 
    titleController.delegate = self; 
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:titleController animated:YES]; 
    [titleController release]; 

我想在風景方向始終顯示模式的看法,即使設備處於縱向舉行。

UPDATE

找到一個可行的解決方案:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
    } 

然而,隨着取向UIInterfaceOrientationPortraitUpsideDown處理時,最初顯示與正確方向的視圖,但然後轉變回一個肖像方向與剪裁的兩側,並顯示圖像上方和下方的背景。

我已經嘗試調整角度爲270,-90和其他值,但總是在面向UIInterfaceOrientationPortraitUpsideDown方向時恢復爲縱向。

爲什麼?

回答

2

這種方法做了我所需要的一切:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
    } 

    TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]]; 
    titleController.delegate = self; 
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:titleController animated:YES]; 
    [titleController release]; 

旋轉之前的方向呈現我的模態觀點。

而我剛剛刪除了支持UIInterfaceOrientationPortraitUpsideDown因爲不需要這個方向。

0

解決方案1:要從初始肖像模式更改啓動方向,您需要編輯您的info.plist文件。爲「初始界面方向」添加條目並設置所需的值。

Here是哪裏可以找到info.plist文件的截圖。(如果該鏈接不起作用,請留言)


解決方案2:你也可以在你的導航控制器作爲一個子視圖到窗口和方向的通知嘗試這隻會被髮送到第一子窗口中的子視圖。

//.m file 
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    // Hide status bar 
    application.statusBarHidden = YES; 
     // --- Add the view controller's view to the window and display. 
    [window addSubview:viewController.view]; 
    [viewController.view addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 
+0

問題是,在最終請求並顯示模態視圖之前,方向可能會改變很多次並處於任何方向。 – GRW 2011-12-16 20:15:18

1

你試過:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

而且當你要關閉它改變爲縱向

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
+0

這似乎在一定程度上工作,但是它確實產生似乎工作(有點)警告 – GRW 2011-12-16 21:02:02

相關問題