仅允许在一个视图控制器上进行自动旋转

编程入门 行业动态 更新时间:2024-10-25 10:34:32
本文介绍了仅允许在一个视图控制器上进行自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目中,我只允许纵向旋转,但对于一个 ViewController 我想启用横向。我将 ViewController 呈现为 ModalViewController ,我尝试使用方法 - (BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 或iOS 6方法,如 - (NSUInteger)supportedInterfaceOrientations 但实际上没有任何效果。虽然这些方法被调用,但视图没有旋转。

In my project I have allowed only portrait rotation, but for one ViewController I would like to enable also landscape. I'm presenting this ViewController as ModalViewController, I've tried using methods - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation or iOS 6 methods like -(NSUInteger) supportedInterfaceOrientations but nothing actually worked. The view didn't rotate although those methods got called.

在此之后,我试图通过听取这些通知的方式将其旋转:

After this I've tried to rotate it by myslef with listening to those notifications :

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

但即使我能够手动旋转视图 in方法 didRotate :它非常混乱,我无法旋转 StatusBar 。

but even though I was able to manually rotate the view in method didRotate: it's very messy and I can't rotate the StatusBar.

我真的很想使用像 shouldAutorotateToInterfaceOrientation 这样的标准方法,但我不知道怎么做。任何人?

I would really like to use standard methods like shouldAutorotateToInterfaceOrientation, but I don't know how. Anyone?

推荐答案

在你的app app.m中添加这个

Add this in your app delegate.m

# pragma mark - Rotation - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([self.window.rootViewController isKindOfClass:[MVYSideMenuController class]]) { // Get topmost/visible view controller UIViewController *currentViewController = [self.window.rootViewController.childViewControllers lastObject]; // Check whether it implements a dummy methods called canRotate if ([currentViewController respondsToSelector:@selector(canRotate)]) { // Unlock landscape view orientations for this view controller return UIInterfaceOrientationMaskAllButUpsideDown; } } // Only allow portrait (standard behaviour) return UIInterfaceOrientationMaskPortrait; } -(void)canRotate { }

然后添加此方法

-(void)canRotate { // just define the method, no code required here }

在每个ViewController(.m文件)中你想提供轮换。你也可以在这里包含 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 方法在设备旋转时做出反应:

in every ViewController (.m files) where you want to provide rotation. You can also include here -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method to react when the device rotates:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; switch (orientation) { case 1: case 2: //NSLog(@"portrait"); break; case 3: case 4: //NSLog(@"landscape"); break; default: //NSLog(@"other"); break; } }

更多推荐

仅允许在一个视图控制器上进行自动旋转

本文发布于:2023-11-14 00:56:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   器上

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!