您的位置:首页 > 移动开发 > IOS开发

iOS强制切换横屏、竖屏

2013-03-04 18:26 323 查看
切换横竖屏最直接的方式是调用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];

来源: <http://blog.csdn.net/dickenslian/article/details/7407221>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: