【问题标题】:How to block rotation in ios 7如何在 iOS 7 中阻止旋转
【发布时间】:2013-10-17 03:11:09
【问题描述】:

我在 ios 7 之前使用此代码阻止旋转(我也使用 xibs,现在是故事板)

- (BOOL)shouldAutorotate {
  return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
 return UIInterfaceOrientationPortrait;
}

现在我迁移到故事板并且 ios7 无法正常工作,我的视图仍在旋转。

更新: 我通过将此代码添加到委托来解决了这个问题,现在我以前的代码就像魅力一样工作

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
 NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
 if (self.fullScreenVideoIsPlaying) {
     return UIInterfaceOrientationMaskAllButUpsideDown;
 }
 else {        
     if(self.window.rootViewController){
         UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
         orientations = [presentedViewController supportedInterfaceOrientations];
 }


return orientations;
  }

【问题讨论】:

    标签: iphone objective-c uiview rotation


    【解决方案1】:

    Atrik 的代码有效。这是一个更完整的解决方案,即使使用 UINavigationController 也允许锁定和解锁仅纵向模式

    appdelegate .h
    
    
    @property (nonatomic) BOOL screenIsPortraitOnly;
    
    
    appdelegate .m
    
    #pragma mark - View Orientation
    
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
        if (self.screenIsPortraitOnly) {
            return UIInterfaceOrientationMaskPortrait;
        }
        else {
            if(self.window.rootViewController){
                UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
                orientations = [presentedViewController supportedInterfaceOrientations];
            }
            return orientations;
        }
    }
    

    对于我需要纵向锁定的所有视图控制器 如果您还没有使用导入了应用程序委托的子类,那么不要忘记导入委托。对于大多数视图控制器,我使用 UIViewController 的子类,它至少可以进行导入。

    #import "AppDelegate.h"
    

    我将它用于所有纵向锁定的视图控制器。

    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:true];
        [self portraitLock];
    }
    
    -(void) portraitLock {
        AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
        appDelegate.screenIsPortraitOnly = true;
    }
    
    #pragma mark - interface posiiton
    
    - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (BOOL) shouldAutorotate {
        return NO;
    }
    

    ViewDidLoad 在 vi​​ewDidAppear 之前运行,所以我在 UIViewController 的子类中运行它来解锁所有屏幕。带有 lock 方法的 viewWillAppear 仅在我需要锁定屏幕的控制器中使用。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self portraitUnLock];
    }
    
    -(void) portraitUnLock {
        AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
        appDelegate.screenIsPortraitOnly = false;
    }
    

    【讨论】:

      【解决方案2】:

      如果您只想要横向模式,那么您可以使用 xcode 项目设置来实现 转到目标>摘要>支持界面方向

      或者你可以做一个代码

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
          // Return YES for supported orientations.
          return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
      }
      

      【讨论】:

        【解决方案3】:

        在需要用于 iOS7 开发的 XCode 5 中,您可以转到您的目标并在部署信息下取消选中除纵向之外的所有设备方向。

        【讨论】:

        • 我无法做到这一点。当我单击横向左侧时,复选框关闭然后立即重新打开。此外,如果我转到 Info.plist 并删除横向方向,它会将它们放回去。
        【解决方案4】:

        如果您根本不希望您的应用旋转(无论哪个视图处于活动状态),您可以在 Xcode 侧边栏中单击您的项目,向下滚动,然后取消选择 Landscape Left 和 Landscape Right。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-06-29
          • 1970-01-01
          • 2019-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多