【问题标题】:Make view controller rotate immediately base on device orientation使视图控制器根据设备方向立即旋转
【发布时间】:2015-09-02 12:06:03
【问题描述】:

我有 2 个视图控制器 A 和 B。视图控制器 A 仅是纵向的,视图控制器 B 可以旋转。

当手机是横向时,我推视图控制器 A,它的方向是纵向 -> 好。

但是当我从视图控制器 A 推送视图控制器 B 时(手机仍然是横向的),视图控制器 B 的方向是纵向的,这不是我想要的。 我必须将设备旋转到纵向然后横向才能使视图控制器 B 旋转到横向。

我的问题是,关于设备方向,我怎样才能让视图控制器 B 在从 A 推送后立即旋转?

【问题讨论】:

    标签: ios


    【解决方案1】:

    使用它可能会解决您的问题

    AppDelegate.m

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    
    return UIInterfaceOrientationMaskLandscape;
    
    }
    

    视图控制器

    -(void) restrictRotation:(BOOL) restriction
    {
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
    }
    

    【讨论】:

    • 抱歉,restrictRotation 是什么?
    【解决方案2】:

    要旋转设备方向,请根据需要在 viewWillAppearviewWillDisAppear 中使用此代码

     NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    

    要旋转视图,请使用此代码

    [self shouldAutorotate];
    [self supportedInterfaceOrientations];
    

    并在ViewController.m

    中定义这个方法
    -(BOOL)shouldAutorotate{
    return YES;
    }
    

    -(NSUInteger)supportedInterfaceOrientations{

        return UIInterfaceOrientationMaskPortrait;    
    }
    

    会有帮助的。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多