无法在 ipad 应用程序中设置方向
Posted
技术标签:
【中文标题】无法在 ipad 应用程序中设置方向【英文标题】:Not able to set orientation in ipad app 【发布时间】:2013-01-16 11:18:46 【问题描述】:我正在使用 ios6 版本来生成新的 Ipad 应用程序。在我的应用程序中,我正在创建一个拆分视图。该拆分视图必须始终处于横向模式。应用程序在 ipad 6.0 模拟器中运行。但不适用于 ipad 5.0 模拟器。我想同时运行 ipad 6.0 和 ipad 5.0 的应用程序。
我正在使用此代码
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskLandscape;
-(BOOL) shouldAutorotate
return NO;
【问题讨论】:
【参考方案1】:你可以在 ios6 中这样做:-
-(BOOL)shouldAutorotate
return YES;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskAll;
并检查每次ViewWillApear
设备方向,如:-
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight)
//set your landscap View Frame
[self supportedInterfaceOrientations];
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown)
//set your Potrait View Frame
[self supportedInterfaceOrientations];
// Handle rotation
-(void)viewWillAppear:(BOOL)animated
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
更新
可能人们使用检查设备orientation
如下方式将此行放入ViewWillApear
:-
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
和
-(void)deviceRotated:(NSNotification*)notification
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
//Do your stuff for landscap
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
//Do your stuff for potrait
仅在 IOS5 中您可以执行如下操作:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
else
return NO;
if you wish to support all oriantation you need to just return YES like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES;
【讨论】:
【参考方案2】:在 iOS 5.0 中你应该使用:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return interfaceOrientation == UIInterfaceOrientationMaskLandscape;
【讨论】:
只需将其放在问题中描述的方法旁边即可。 我认为这行不通。那行应该是:return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); UIInterfaceOrientationMaskLandscape 定义为 (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)。查看 UIApplication 类参考。以上是关于无法在 ipad 应用程序中设置方向的主要内容,如果未能解决你的问题,请参考以下文章