ios单独的页面支持横竖屏的状态调整,HTML5加载下(更新2)

单独的页面支持横竖屏的状态调整,HTML5加载下

工程中设置只支持竖屏状态,在加载HTML5的界面可以是横竖屏的,在不对工程其他界面/设置做调整的同时,可以这样去

#import "ViewController.h"

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

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()<UIWebViewDelegate>
{
    UIWebView *webview;
    
    
    
    UIButton * back;

    
}


@end


@implementation ViewController


//此状态一定要是 NO  不然无法对旋转后的尺寸进行适配
-(BOOL)shouldAutorotate{
    
    return NO;
}
//支持的状态
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 如果该界面需要支持横竖屏切换
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
  
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    self.tabBarController.tabBar.hidden = YES;
    
//    self.navigationController.navigationBar.hidden= NO;
//    
//    
//    //隐藏 状态栏
//    [[UIApplication sharedApplication]setStatusBarHidden:YES];
//    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBar.hidden= NO;
//    [[UIApplication sharedApplication]setStatusBarHidden:NO];
//    
    
}

- (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 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.cnblogs.com/xujiahui/p/6583204.html"]]]];
    webview.delegate = self;
    
    webview.scalesPageToFit = YES;
    // webview.scrollView.scrollEnabled = NO;
    [self.view addSubview:webview];
    
    //back。是一个button
    //back = [myButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.view.frame.size.width-80, self.view.frame.size.height-100, 40,40) tag:1 image:nil andBlock:^(myButton *button) {
    
    back= [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80, self.view.frame.size.height-100, 40,40)];
    
    
    [self.navigationController popViewControllerAnimated:YES];
  
    back.backgroundColor = [UIColor greenColor];
    
    
    
    [self.view addSubview:back];
    
    
}



//横屏//横屏  状态下  width和height是颠倒的
- (void)deviceOrientationDidChange
{
    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
    
    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        
        [UIView animateWithDuration:0.3f 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-80, SCREEN_HEIGHT-100, 60, 60);
            
            
        }];
        
        
        
        
        //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ) {
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
        
        [UIView animateWithDuration:0.3f 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-80, SCREEN_HEIGHT-10, 60, 60);
            
            
        }];
        
        
        
        
    }else if ( [UIDevice currentDevice].orientation== UIDeviceOrientationLandscapeRight){
        
        
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
        
        [UIView animateWithDuration:0.3f 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-80, SCREEN_HEIGHT-100, 60, 60);
            
            
        }];
    }
}


@end
原文地址:https://www.cnblogs.com/xujiahui/p/6583204.html