2014-08-28 27 views
1

比方說,我有一個視圖控制器,稱之爲ViewControllerPortrait,它被設計爲只能在肖像模式下顯示。例如: -如何在設備旋轉過程中「鎖定」視圖的方向

ViewControllerPortrait

當用戶旋轉設備的風景,我不想ViewControllerPortrait永遠重新定位自己的風景,但我想提出一個新的全屏視圖。我們稱之爲全屏視圖控制器ViewControllerLandscapeFull。例如:

enter image description here

我再也不想看到的是這樣的:

Don't do this

我試圖做到這一點的方法是讓窗口的RootViewController的存在ViewControllerLandscapeFull在當屏幕全屏時willRotateToInterfaceOrientation:duration:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
[navigationViewController setNavigationBarHidden:YES animated:YES]; 

self.viewControllerPortrait = [[ViewControllerPortrait alloc] init]; 
self.viewControllerPortrait.modalPresentationStyle = UIModalPresentationFullScreen; 
[self presentViewController:self.viewControllerPortrait animated:NO completion:NULL]; 

然後在ViewControllerLandscapeFull我:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return (UIInterfaceOrientation)[UIDevice currentDevice].orientation; 
} 

這工作得相當好。由於ViewControllerLandscapeFull「更喜歡」風景,並具有UIModalPresentationFullScreenmodalPresentationStyle,它只能在風景中顯示。通過爲supportedInterfaceOrientations返回landscape | portrait,設備旋轉回肖像被允許發生ViewControllerLandscapeFull得到willRotateToInterfaceOrientation:duration:並最終調用dismissViewControllerAnimated:NO

我唯一的問題是,在旋轉回肖像期間,您可以暫時看到橫向的ViewControllerPortrait,這就是我想要避免的。

ViewControllerPortrait確實實現:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

而且我驗證過的UIKit是調用它,但它似乎沒有任何效果。 (該文檔說,supportedInterfaceOrientations只在根視圖控制器和視圖控制器呈現全屏,所以我實際上很驚訝UIKit稱之爲。)

FWIW,我正在與iOS 8 beta 5 SDK並建設7/8。

Muchas gracias。

+0

你有螞蟻tabbar或導航欄在你的應用程序?我的意思是你不想旋轉的視圖控制器在任何tabbar或navigationcontroller內? – Rashad 2014-08-28 06:45:20

+0

是的,我所稱的ViewControllerPortrait是UINavigationController(實際上是一個子類)的一部分。 – 2014-08-28 19:40:05

回答

0

我剛剛開始使用iPhone編程,但也許這將工作...

點擊您的項目文件>部署信息。然後,除了肖像以外,請取消選中所有內容這樣旋轉設備將不會重新定位自己。

但是,你仍然可以測試設備是否旋轉或不符合本:

- (BOOL)shouldAutorotate 
{ 
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
    { 
     NSLog(@"ROTATE to landscape"); 
     kitty.hidden = NO; 
     //(kitty is my imageview, hidden on load), once the device rotates to landscape, you can show your image.(or you can do something else) 
    } 
    else{ 
     NSLog(@"ROTATE to portrait"); 
     kitty.hidden = YES; 
    } 
    return YES; 
} 
+0

創新的解決方案,但它不適合我。只要我使用首選的橫向方向呈現新​​的視圖控制器,ViewControllerPortrait的viewWillLayoutSubviews就會以翻轉的邊界被調用。我也可以像以前一樣暫時在風景中看到它。 – 2014-08-28 19:53:10

0
houldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 

上述方法不會被調用一個視圖 - 控制的,如果他們是navigationcontroller的任何tabbarcontroller內。如果這些方法在tabbarcontroller或導航控制器內部聲明,那麼它們將被調用。在我的例子中,viewcontrollers在navigationcontroller裏面,導航控制器在tabbarcontroller裏面。

爲了解決這個我做一個類FixedOrientationTab,它是UITabBarController一個子類,導航類OrientationEnabledNavigation,它是UINavigationController一個子類。然後我在FixedOrientationTabOrientationEnabledNavigation內實施shouldAutorotate,supportedInterfaceOrientations,preferredInterfaceOrientationForPresentation方法。

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h> 

@interface OrientationEnabledNavigation : UINavigationController 

@end 

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h" 

@interface OrientationEnabledNavigation() 

@end 

@implementation OrientationEnabledNavigation 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (BOOL)shouldAutorotate 
{ 
    return [self.topViewController shouldAutorotate]; 
// return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

FixedOrientationTab.h

#import <UIKit/UIKit.h> 

@interface FixedOrientationTab : UITabBarController 

@end 

FixedOrientationTab.m

#import "FixedOrientationTab.h" 

@interface FixedOrientationTab() 

@end 

@implementation FixedOrientationTab 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (BOOL)shouldAutorotate 
{ 
    return [self.selectedViewController shouldAutorotate]; 
    // return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.selectedViewController supportedInterfaceOrientations]; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [self.selectedViewController preferredInterfaceOrientationForPresentation]; 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

然後,如果你想在你的項目中使用導航控制器,然後使用OrientationEnabledNavigation和的TabBar FixedOrientationTab。之後,如果您在viewcontroller中執行shouldAutorotatesupportedInterfaceOrientationspreferredInterfaceOrientationForPresentation這些方法,那麼它們將被調用。

希望這會有所幫助.. :)

相關問題