2014-08-28 124 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这些方法,那么它们将被调用。

希望这会有所帮助.. :)

相关问题