iOS横屏、竖屏切换

切换横竖屏最直接的方式是调用device的setOrientation方法。但是从sdk3.0以后,这个方法转为似有API,如果要上AppStore的话,要慎用!

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

        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)

                                       withObject:(id)UIInterfaceOrientationLandscapeRight];

    }

第二种方式是手动的设置界面元素的旋转,包括状态栏、导航栏和视图。以下代码为从竖屏设置为横屏,坐标系是以竖屏的为基准,所以会出现负数的坐标值。

    //设置状态栏旋转

    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;

    //设置旋转动画

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:duration];    

    //设置导航栏旋转

    self.navigationController.navigationBar.frame = CGRectMake(-204, 224, 480, 32);

    self.navigationController.navigationBar.transform = CGAffineTransformMakeRotation(M_PI*1.5);

    //设置视图旋转

    self.view.bounds = CGRectMake(0, -54, self.view.frame.size.width, self.view.frame.size.height);

    self.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);

    [UIView commitAnimations];




你可能感兴趣的:(ios,api)