iOS 横屏问题

第一种

- (BOOL)shouldAutorotate {
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationLandscapeRight;
}

第二种把view横屏

 UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView animateWithDuration:duration animations:^{
        
        if (orientation == UIInterfaceOrientationLandscapeLeft)
        {
            self.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }
        else if (orientation == UIInterfaceOrientationLandscapeRight)
        {
            self.transform = CGAffineTransformMakeRotation(M_PI/2);
        }
        
    }completion:^(BOOL finished) {
        
    }];
    
    self.frame = keyWindow.bounds;
    [self setNeedsLayout];
    [self layoutIfNeeded];

第三种

@interface AppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow * window;

@property (nonatomic, assign) BOOL allowRotation;//允许旋转
@property (nonatomic, assign) BOOL rightRotation;//右侧旋转


@end
@implementation AppDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
   
   if (self.allowRotation)
   {
     return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
   }
   else if(self.rightRotation)
   {
       return UIInterfaceOrientationMaskLandscapeRight;
   }
   return UIInterfaceOrientationMaskPortrait;
}
@end

//在对应的viewController
//全屏
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
   appdelegate.rightRotation=YES;
   [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
   //强制翻转屏幕,Home键在右边。
   [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
   //刷新
   [UIViewController attemptRotationToDeviceOrientation];

//退出全屏
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
   appdelegate.rightRotation=NO;
   [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
   //强制翻转屏幕//UIDeviceOrientationPortrait
   [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
   //刷新
   [UIViewController attemptRotationToDeviceOrientation];

你可能感兴趣的:(iOS 横屏问题)