iOS强制切换横屏、竖屏

来源:互联网 发布:网络上信息安全问题 编辑:程序博客网 时间:2024/05/21 06:18

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

    if ([[UIDevice currentDevicerespondsToSelector:@selector(setOrientation:)]) {

        [[UIDevice currentDeviceperformSelector:@selector(setOrientation:)

                                       withObject:(id)UIInterfaceOrientationLandscapeRight];

    }

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

    //设置状态栏旋转

    [[UIApplication sharedApplicationsetStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;

    //设置旋转动画

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:duration];    

    //设置导航栏旋转

    self.navigationController.navigationBar.frame = CGRectMake(-20422448032);

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

    //设置视图旋转

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

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

    [UIView commitAnimations];


来源: <http://blog.csdn.net/dickenslian/article/details/7407221>
原创粉丝点击