gpt4 book ai didi

ios - shouldAutorotate 未在 View Controller 上调用

转载 作者:可可西里 更新时间:2023-11-01 05:11:28 27 4
gpt4 key购买 nike

我有一个由单个 View Controller 组成的简单应用程序。我从 Xcode 7 GM 单 View 应用程序模板开始,但随后删除了主 Storyboard,并像这样设置我的 View Controller :

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 中,我在 Supported Interface Orientations 下指定了所有方向,并且应用程序旋转到 iPad 上的所有方向。

但是,在我的简单 View Controller 中,永远不会调用 shouldAutorotate()supportedInterfaceOrientations() 方法。这是一个问题,因为我正在试验启用和禁用自动旋转的 UI 控件。是什么阻止了调用这些方法?

示例项目 here (需要 Swift 2)


¹非UINavigationController

最佳答案

根据 Apple Developer 论坛上的这篇帖子,如果您启用了 iPad 多任务处理(iOS 9 中的新功能),您将无法再控制所支持的方向:

https://forums.developer.apple.com/message/13508#13508

您可以添加反向旋转,但它并不漂亮,至少据我所知。我让它工作了,但是当你旋转时无法禁用角的动画,所以你会看到这种看起来很奇怪的情况,它看起来像在旋转,但内容没有旋转。

这是我用来对抗旋转的代码。请注意,我也必须隐藏状态栏,否则它也会旋转,我不知道如何解决这个问题。

另请注意,在 self.navigationController.view.superview.superview 上进行自动旋转可能不是最好的方法,并且可能会在将来的某个时候中断。可能有更好的方法来获得用于对抗旋转的正确 View 。显然,如果您不使用导航 Controller ,则需要传入不同的 View 。 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(经他许可发布),可在 GitHub 上根据 MIT 许可获得:https://github.com/jaredsinclair/JTSImageViewController

关于ios - shouldAutorotate 未在 View Controller 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com