屏幕旋转

在创建的vc中

 //指定能够支持的orientation有哪些

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{

    //默认的情况下是maskAllButUpSideDown

    return UIInterfaceOrientationMaskAll;

}

//解决statusBar在翻转过程消失

-(BOOL)prefersStatusBarHidden{

    return NO;

}

//强制旋转方法一 不会改变statusbar

  self.view.transform=CGAffineTransformMakeRotation(M_PI_2);

//强制旋转方法二 设置固定值

  NSNumber* value=[NSNumber numberWithInteger:UIDeviceOrientationPortrait];

   [[UIDevice currentDevice]setValue:value forKey:@"orentation"];

设备或interface旋转的通知

//添加observer来观察通知, selector需要自己实现, name是系统封装好的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DeviceOrientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

//Interface通知 UIApplicationStatusBar很形象 状态栏的方向改变

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(InterfaceOritationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

//键盘相关

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeybordDidShow:) name:UIKeyboardDidShowNotification object:nil];

屏幕发生旋转时改变布局

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator{

    //第一个block表示正在旋转中 第二个block就是选装结束了

    [coordinatoranimateAlongsideTransition:^(id  _Nonnull context) {

        if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))

        {//do layout}

        else{//do layout}

        }completion:^(id  _Nonnull context) {

        NSLog(@"screen boundes %@",NSStringFromCGRect(self.view.bounds));

    }];

    //需要调用super的默认方法 要不就不向子类传递了

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

}

//同样的事情也可以在-(void)viewDidLayoutSubviews来做

-(void)viewDidLayoutSubviews{

     if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))        {//do layout}        else{//do layout}

}


你可能感兴趣的:(屏幕旋转)