【问题标题】:unable to stop device rotation无法停止设备旋转
【发布时间】:2014-04-20 07:14:44
【问题描述】:

我已经发布了一个问题here。其中一位哥们回答并给出了解决方案。尽管该解决方案适用于少数视图控制器,但在视图中它不起作用。当我在选项卡栏项上输入具有 TabController + 导航控制器的视图控制器时,代码不起作用。并且视图可以旋转。

我在 iOS 6.1 中使用了以下代码

//For iOS6
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;
}

我也必须在 iOS 7 中实现相同的功能。 请帮忙

【问题讨论】:

  • 你想要什么?停止所有视图控制器的旋转或停止少数视图控制器的旋转?
  • @Rashad 我想禁用所有旋转,除了我必须在横向和纵向模式下显示 MPMoviePlayerController 的地方

标签: ios uinavigationcontroller uitabbarcontroller ios6.1


【解决方案1】:
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

如果上述方法在navigationcontroller 的任何tabbarcontroller 内,则不会调用viewcontroller。如果这些方法在 tabbarcontroller 或导航控制器中声明,那么它们将被调用。

为了解决这个问题,创建一个类FixedOrientationTab,它是UITabBarController 的子类,导航类OrientationEnabledNavigation,它是UINavigationController 的子类。然后在FixedOrientationTab和OrientationEnabledNavigation内部实现shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation方法。

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
//    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

FixedOrientationTab.h

#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end

FixedOrientationTab.m

#import "FixedOrientationTab.h"

@interface FixedOrientationTab ()

@end

@implementation FixedOrientationTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
    //    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

如果您想在项目中使用导航控制器,请使用OrientationEnabledNavigation 和标签栏FixedOrientationTab。之后,如果您在 viewcontroller 中实现 shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 这些方法,那么它们将被调用。

希望这会有所帮助.. :)

编辑 正如 Jeev 指出的另一种方法是在您的应用程序委托的开头添加一些代码:

对于UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.selectedViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    else {
        return UIInterfaceOrientationMaskAll;
    }
}

@end

对于UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.topViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end

【讨论】:

  • 非常感谢您的详细解释。让我检查一下,我会在几分钟内回复,,,再次感谢
  • 我从过去 6 个月开始一直在 dvlpmnt,但我无法正确实施你上面提到的方法。此外,我读到一些苹果放弃了继承 UitabBarController 的想法。然后我搬家了。然后我找到了一个在kurutepe.com/2013/02/… 上创建类别的解决方案,它对 iOS 6 和 7 都适用。该解决方案与您的解决方案非常相似.但是我没有在我的项目中创建一个不同的 UIatbarclass,只是在 AppDelegate 类中创建它......非常感谢
  • 谢谢。这就是我喜欢SO的原因。学到了一个新东西..我也要把这个添加到我的答案中。它会帮助别人。 :)
【解决方案2】:

我有 UINavigationController 包含 UIViewController。

您应该通过子类 UINavigationController 禁用 UINavigationController 中的旋转并实现

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
// return some
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
// return some thing that you want
}

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    由于我使用的是 TabBarController,我得到的唯一可行的解​​决方案是创建一个类别。

    所以只需创建一个类别

    这是该类别的 .h 类,我将其命名为 AutoRotationtab

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    
    @interface UITabBarController (AutoRotationTab)
    
    @end
    

    这是该类别的 .m 类

    #import "UITabBarController+AutoRotationTab.h"
    
    @implementation UITabBarController (AutoRotationTab)
    
    -(BOOL)shouldAutorotate
    {
    if(globalvariable_shouldRotate)
    {
        return YES;
    }
    else
    {
        return NO;
    }
    }
    
    
    - (NSUInteger)supportedInterfaceOrientations
    {
    
    if(globalvariable_shouldRotate)
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    }
    

    只要创建这个类别,看看魔法,它就起作用了

    【讨论】:

      猜你喜欢
      • 2013-02-20
      • 2013-02-07
      • 2015-06-15
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2012-03-27
      相关资源
      最近更新 更多