iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转

Posted 徐家汇123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转相关的知识,希望对你有一定的参考价值。

只含有 。关于横竖屏的代码

 

#import "InspectionReportViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface InspectionReportViewController ()<UIWebViewDelegate>
{
    UIWebView *webview;

    UIButton * back;
 
}
@end

@implementation InspectionReportViewController

-(BOOL)shouldAutorotate{
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 如果该界面需要支持横竖屏切换
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
    // 如果该界面仅支持横屏
    // return UIInterfaceOrientationMaskLandscapeRight;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
}

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

[self CreatUI];
 
    //横屏同志UIApplicationDidChangeStatusBarFrameNotification   UIDeviceOrientationDidChangeNotification
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}

-(void)CreatUI{
    
    self.view.backgroundColor = [UIColor blackColor];
    
    webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webview.backgroundColor = [UIColor blackColor];
    
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://60.205.188.204:8087/WebDcmviewer/dcmviewer.html?org_code=%@&check_id=%@",_org_code,_check_id]]]];

    webview.scalesPageToFit = YES;
    webview.scrollView.scrollEnabled = NO;
    [self.view addSubview:webview];
    
  
     back = [myButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(ScreenWidth-IPHONEWIDTH(120), ScreenHeight-IPHONEHIGHT(100), IPHONEWIDTH(50), IPHONEHIGHT(50)) tag:1 image:@"ic_history_ct_return" andBlock:^(myButton *button) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
        
    }];

    [self.view addSubview:back];
    
    
}



//横屏
- (void)deviceOrientationDidChange
{
    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
    
    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        
        [UIView animateWithDuration:0.1f animations:^{
            
            self.view.transform = CGAffineTransformMakeRotation(0);
       self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
      
            webview.frame = self.view.bounds;
            
            back.frame = CGRectMake(SCREEN_WIDTH-IPHONEWIDTH(80), SCREEN_HEIGHT-IPHONEHIGHT(100), IPHONEHIGHT(60), IPHONEHIGHT(60));
            
   
        }];

        
       
        
        //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ) {
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
      
        [UIView animateWithDuration:0.1f animations:^{
            
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
            
            self.view.bounds = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);

            webview.frame = self.view.bounds;
            
            back.frame = CGRectMake(SCREEN_WIDTH-IPHONEWIDTH(80), SCREEN_HEIGHT-IPHONEHIGHT(100), IPHONEHIGHT(60), IPHONEHIGHT(60));
   
            
        }];

        

       
    }else if ( [UIDevice currentDevice].orientation== UIDeviceOrientationLandscapeRight){
        
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
        
        [UIView animateWithDuration:0.1f animations:^{
            
            self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
            
         self.view.bounds = CGRectMake(0, 0,SCREEN_HEIGHT, SCREEN_WIDTH);
 
            webview.frame = self.view.bounds;
            
            back.frame = CGRectMake(SCREEN_WIDTH-IPHONEWIDTH(80), SCREEN_HEIGHT-IPHONEHIGHT(100), IPHONEHIGHT(60), IPHONEHIGHT(60));


        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

以上是关于iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转的主要内容,如果未能解决你的问题,请参考以下文章

iOS横竖屏设置

Swift 支持某个页面横竖屏切换与强制横屏

CocosCreator设置模拟器默认横竖屏以及机型

uniapp 横竖屏设置

【iOS】Swift4.0 横竖屏监测、动态切换

苹果电脑横屏怎么调竖屏