shouldAutorotate 未在视图控制器上调用
shouldAutorotate not called on view controller
我有一个由单个视图控制器组成的简单应用程序。我从 Xcode 7 GM Single View Application 模板开始,但随后删除了主故事板,并像这样设置我的视图控制器:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let vc = ViewController()
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = vc
window!.makeKeyAndVisible()
return true
}
在我的信息 plist 中,我在支持的界面方向下指定了所有方向,应用程序旋转到 iPad 上的所有方向。
然而,在我的简单视图控制器中,shouldAutorotate()
和 supportedInterfaceOrientations()
方法从未被调用。这是一个问题,因为我正在试验 UI 控件启用和禁用自动旋转。是什么阻止了调用这些方法?
示例项目here(需要Swift 2)
¹非UINavigationController
根据 Apple Developer 论坛上的 post,如果您启用了 iPad 多任务处理(iOS 9 中的新功能),您将无法再控制您支持的方向:
https://forums.developer.apple.com/message/13508#13508
您可以添加反向旋转,但它并不漂亮,至少据我所知是这样。我让它工作了,但是当你旋转时无法禁用角的动画,所以你会看到这种看起来很奇怪的情况,它看起来像在旋转,但内容没有旋转。
这是我用来对抗旋转的代码。请注意,我也必须隐藏状态栏,否则它也会旋转,我不知道如何解决这个问题。
另请注意,在 self.navigationController.view.superview.superview
上进行自动旋转可能不是最好的方法,并且可能会在将来的某个时候中断。可能有更好的方法来获得用于对抗旋转的正确视图。显然,如果您不使用导航控制器,则需要传入不同的视图。 YMMV.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.startingInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self updateLayoutsForCurrentOrientation:toInterfaceOrientation view:self.navigationController.view.superview.superview];
}
- (void)updateLayoutsForCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation view:(UIView *)view {
CGAffineTransform transform = CGAffineTransformIdentity;
if (self.startingInterfaceOrientation == UIInterfaceOrientationPortrait) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformIdentity;
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
default:
break;
}
}
view.transform = transform;
}
大部分代码改编自 Jared Sinclair 的 JTSImageViewController(post 已获得他的许可),根据 MIT 许可可在 github 此处获得:https://github.com/jaredsinclair/JTSImageViewController
我刚刚尝试了这个线程中的解决方案:
请注意 info.plist 中有两个条目,一个用于 iPhone 支持的方向,另一个用于 iPad。
要选择不再有资格参与 Slide Over 和 Split View,请将 UIRequiresFullScreen 键添加到您的 Xcode 项目的 Info.plist 文件并应用布尔值 YES。
我有一个由单个视图控制器组成的简单应用程序。我从 Xcode 7 GM Single View Application 模板开始,但随后删除了主故事板,并像这样设置我的视图控制器:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let vc = ViewController()
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = vc
window!.makeKeyAndVisible()
return true
}
在我的信息 plist 中,我在支持的界面方向下指定了所有方向,应用程序旋转到 iPad 上的所有方向。
然而,在我的简单视图控制器中,shouldAutorotate()
和 supportedInterfaceOrientations()
方法从未被调用。这是一个问题,因为我正在试验 UI 控件启用和禁用自动旋转。是什么阻止了调用这些方法?
示例项目here(需要Swift 2)
¹非UINavigationController
根据 Apple Developer 论坛上的 post,如果您启用了 iPad 多任务处理(iOS 9 中的新功能),您将无法再控制您支持的方向:
https://forums.developer.apple.com/message/13508#13508
您可以添加反向旋转,但它并不漂亮,至少据我所知是这样。我让它工作了,但是当你旋转时无法禁用角的动画,所以你会看到这种看起来很奇怪的情况,它看起来像在旋转,但内容没有旋转。
这是我用来对抗旋转的代码。请注意,我也必须隐藏状态栏,否则它也会旋转,我不知道如何解决这个问题。
另请注意,在 self.navigationController.view.superview.superview
上进行自动旋转可能不是最好的方法,并且可能会在将来的某个时候中断。可能有更好的方法来获得用于对抗旋转的正确视图。显然,如果您不使用导航控制器,则需要传入不同的视图。 YMMV.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.startingInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self updateLayoutsForCurrentOrientation:toInterfaceOrientation view:self.navigationController.view.superview.superview];
}
- (void)updateLayoutsForCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation view:(UIView *)view {
CGAffineTransform transform = CGAffineTransformIdentity;
if (self.startingInterfaceOrientation == UIInterfaceOrientationPortrait) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformIdentity;
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
default:
break;
}
}
else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(M_PI);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortrait:
transform = CGAffineTransformMakeRotation(M_PI/2.0f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(-M_PI/2.0f);
break;
default:
break;
}
}
view.transform = transform;
}
大部分代码改编自 Jared Sinclair 的 JTSImageViewController(post 已获得他的许可),根据 MIT 许可可在 github 此处获得:https://github.com/jaredsinclair/JTSImageViewController
我刚刚尝试了这个线程中的解决方案:
请注意 info.plist 中有两个条目,一个用于 iPhone 支持的方向,另一个用于 iPad。
要选择不再有资格参与 Slide Over 和 Split View,请将 UIRequiresFullScreen 键添加到您的 Xcode 项目的 Info.plist 文件并应用布尔值 YES。