所有视图都是竖屏,一个ViewController需要强制转换横屏

1.info.plist 里Supported interface orientations添加横屏和竖屏的方向。(在Target里打钩也可以)
2.在self.window的rootViewController类里写下面三个方法:(因为我所有视图都是竖屏不支持旋转的,所以关闭了自动旋转)
- ( BOOL )shouldAutorotate
{
     return  NO ;
}
- (
NSUInteger )supportedInterfaceOrientations
{
   
  return  UIInterfaceOrientationMaskAll ;
}
- (
UIInterfaceOrientation )preferredInterfaceOrientationForPresentation
{
   
  return  UIInterfaceOrientationPortrait ;
}

3.在需要旋转横屏的ViewController里,写下面的代码

//先恢复原状

    self.view.transform = CGAffineTransformIdentity;

    //隐藏导航栏

    self.navigationController.navigationBarHidden = YES;

    //隐藏状态栏

    [UIApplication sharedApplication].statusBarHidden = YES;

        

    //设置bounds

    self.view.bounds = CGRectMake(0, 0, HDScreenWidth, HDScreenHeight);


    //旋转view

    [self.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];

    

    //设置状态栏方向,超级重要。(只有设置了这个方向,才能改变弹出键盘的方向)

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];


私有API,设置设备方向。

//设置状态栏横屏

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

//    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//这句话是防止手动先把设备置为横屏,导致下面的语句失效.

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

        [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:@(UIInterfaceOrientationLandscapeRight)];

    }


你可能感兴趣的:(ios,私有方法,屏幕旋转,强制转屏)