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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM