iOS 横屏问题

作者: 断念的决绝 | 来源:发表于2021-08-22 20:04 被阅读0次

    第一种

    - (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 <UIApplicationDelegate>
    
    @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 横屏问题

        本文链接:https://www.haomeiwen.com/subject/zushiltx.html