我的应用是UITabBarController - > UINavigationController - > UITableViewController - >的UIViewController。
我想做两件事:
阻止我的桌面视图旋转,我希望它始终保持纵向。
FORCE&允许我的UIViewcontroller旋转landscapeleft。
我所知道的:
据我所知,层次结构顶部的viewcontroller控制着旋转。这将是我的UITabBarController?或者更确切地说,它唯一的viewcontroller将在objectIndex:0?
我的项目设置允许纵向,LL和LR旋转。我认为这是我需要遵循的模式,以解决这个问题,允许顶层的ALL旋转,然后单独控制每个vc,对吗?
这是我迄今为止在SO中找到的。
因此,对于我的顶级层次结构,我将项目设置设置为允许旋转到Portrait,LL和LR。
然后在我不想旋转的tableviewcontroller中:
-(BOOL)shouldAutorotate{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
最后在我想要旋转的uiviewcontroller中:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate{
return YES;
}
然而,这不起作用。我可以向任何方向旋转。我也不知道当我到达我的uvc时如何强制旋转LL,这是从我的tablevc的模态segue中调用的。
任何有助于理解这一混乱的人都会非常感激:)
答案 0 :(得分:12)
简单但工作得很好。 IOS 7.1和8
AppDelegate.h
@property () BOOL restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
的ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidLoad中
[self restrictRotation:YES]; or NO
答案 1 :(得分:6)
好的,这是。有点复杂。
项目设置必须允许P, LL & LR
故事板是一个UINavController
,其中UITableViewController
的推杆按钮位于UIViewController
。
故事板中的所有场景必须在模拟指标中推断为方向。只是说,经过一段时间后我会在测试之后将它们全部用于不同的设置。
必须有NavController
,TableViewController
和给定UIVController
的类。我的应用程序以单视图开始,然后我拖入UITVC并最终将UITVC嵌入到UINVC中。然后我将UIVC连接到我通过push segue拖入的UITVC栏按钮项目。
将应用window.rootvc
设置为navVC
,即您的顶级层次结构vc(不要忘记在故事板中设置该ID,否则当然会崩溃):
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"];
self.window.rootViewController = myNavC;
告诉大老板UINVC每个人都可以轮换:
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
限制tablevc以使其不会旋转:
- (BOOL)shouldAutorotate { return NO; }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait);
}
允许上一个UIVC旋转:
- (BOOL)shouldAutorotate { return YES; }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskAllButUpsideDown);
}
答案 2 :(得分:0)
性反应的
不应该混淆UIDeviceOrientation和界面方向这里是我的解决方案
Appdelegate.h
@property () UIInterfaceOrientationMask restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIInterfaceOrientationMask restrictionOrientation = 0;
if (_restrictRotation == 0)
return UIInterfaceOrientationMaskAll;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
switch (currentOrientation)
{
case UIDeviceOrientationPortrait:
if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation))
restrictionOrientation = UIInterfaceOrientationMaskAll;
else
restrictionOrientation = UIInterfaceOrientationMaskPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation))
restrictionOrientation = UIInterfaceOrientationMaskAll;
else
restrictionOrientation = UIInterfaceOrientationMaskPortrait;
break;
case UIDeviceOrientationLandscapeLeft:
if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation))
restrictionOrientation = UIInterfaceOrientationMaskAll;
else
restrictionOrientation = UIInterfaceOrientationMaskPortrait;
break;
case UIDeviceOrientationLandscapeRight:
if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation))
restrictionOrientation = UIInterfaceOrientationMaskAll;
else
restrictionOrientation = UIInterfaceOrientationMaskPortrait;
break;
case UIDeviceOrientationUnknown:
if (!(UIInterfaceOrientationUnknown & _restrictRotation))
restrictionOrientation = UIInterfaceOrientationMaskAll;
else
restrictionOrientation = UIInterfaceOrientationMaskPortrait;
break;
default:
NSLog(@"Unknown orientation");
break;
}
return restrictionOrientation;
}
在每个vc中添加函数
-(void) restrictRotation:(UIInterfaceOrientationMask) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidAppear
//
// Set vc orientation
//
[self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight];
or what you want.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
or what you want.