由axiixc建议的解决方案通常更好,这取决于你的问题,你应该look at his answer。但是,这并没有解决我的问题。我有一个UINavigationController
,其中我将视图控制器的视图作为索引0的子视图插入。这样,视图就位于后面。然后我做了可拖动的拖拽,这样当它拖拽时,它会在后面显示视图。然后,最前面的视图将随导航栏一起移动。 正如axiixc所建议的那样,我没有设法让它与视图控制器遏制API引入iOS 5一起工作。
取而代之,我从导航控制器中删除了视图,并在启动应用程序时将其直接添加到UIWindow
,我自己处理了此视图的旋转并禁用了所有其他视图的旋转(即,我禁用了该视图导航控制器)。
这是我如何添加视图-application:didFinishLaunchingWithOptions:
self.revealViewController = [[RevealViewController alloc] init];
[self.window insertSubview:self.revealViewController.view atIndex:0];
后,立刻,我注册了UIDeviceOrientationDidChangeNotification
。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
我didRotate:
看起来是这样的:
- (void)didRotate:(NSNotification *)notification
{
// Only rotate if the view is revealed
if (self.revealNavigationController.isViewRevealed)
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
[self updateForDeviceOrientation:orientation animated:YES];
}
}
我打电话-updateForDeviceOrientation:animated:
- (void)updateForDeviceOrientation:(UIDeviceOrientation)orientation animated:(BOOL)animated
{
CGFloat degrees = 0.0f;
switch (orientation)
{
case UIDeviceOrientationLandscapeLeft:
degrees = 90.0f;
break;
case UIDeviceOrientationLandscapeRight:
degrees = -90.0f;
break;
case UIDeviceOrientationPortrait:
degrees = 0.0f;
break;
default:
break;
}
CGFloat duration = (animated) ? [UIApplication sharedApplication].statusBarOrientationAnimationDuration : 0.0f;
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:duration animations:^{
// Rotate view
weakSelf.revealViewController.view.transform = CGAffineTransformIdentity;
weakSelf.revealViewController.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
// Resize view for rotation
CGFloat width, height;
if (UIDeviceOrientationIsLandscape(orientation))
{
width = MAX(CGRectGetWidth(weakSelf.revealViewController.view.bounds), CGRectGetHeight(weakSelf.revealViewController.view.bounds));
height = MIN(CGRectGetWidth(weakSelf.revealViewController.view.bounds), CGRectGetHeight(weakSelf.revealViewController.view.bounds));
}
else
{
width = MIN(CGRectGetWidth(weakSelf.revealViewController.view.bounds), CGRectGetHeight(weakSelf.revealViewController.view.bounds));
height = MAX(CGRectGetWidth(weakSelf.revealViewController.view.bounds), CGRectGetHeight(weakSelf.revealViewController.view.bounds));
}
CGSize newSize = CGSizeMake(width, height);
CGRect viewBounds = weakSelf.revealViewController.view.bounds;
viewBounds.size = newSize;
weakSelf.revealViewController.view.bounds = viewBounds;
CGRect viewFrame = weakSelf.revealViewController.view.frame;
viewFrame.size = newSize;
weakSelf.revealViewController.view.bounds = viewFrame;
}];
}
是(这是由在计算器上另一个答案,我现在无法再次找到,遗憾的启发)你的问题是,用户可以揭示后视图,侧向旋转,然后隐藏它,主视图仍然横向,或者主视图即使在屏幕外时也会旋转? – axiixc 2013-04-30 20:26:14
@axiixc问题在于主视图在屏幕外旋转。视图控制器并非真正支持方向更改,因此如果您显示后视图,旋转到横屏,旋转回肖像并再次隐藏后视图以显示主视图,主视图会混乱。理想情况下,我希望主要视图不可能旋转。这将解决你提到的两个问题。因此,如果用户显示后视图,侧向旋转并隐藏它,主视图仍然是肖像。 – simonbs 2013-04-30 20:30:29