ios – 如何根据开关按钮ON和OFF更改设备的方向?

前端之家收集整理的这篇文章主要介绍了ios – 如何根据开关按钮ON和OFF更改设备的方向?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在应用运行时更改应用方向.我的过程是,如果我打开开关,应用程序应该改变其方向(即)(i)如果应用程序以横向方向运行,那么如果我打开“开启”按钮,则设备方向应自动更改为纵向模式. .(ii)如果应用程序在纵向模式下运行,如果我关闭开关,那么设备方向应该自动改变为横向模式…我已经尝试了一些东西,但它不是为我而行..可以任何人请给出一些解决方案为了这..

- (IBAction)PortraitSwitchPressed:(UISwitch *)sender
   {
     if (sender.isOn)
     {
        UIInterfaceOrientationMaskPortrait;
        [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationPortrait]; // no visible @interface for 'UIDevice' declares the selector setorientation
          [self.PortraitSwitch setOn:YES animated:YES];
      }
else
     {

       UIInterfaceOrientationMaskLandscape;
      [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationMaskLandscape];
         [self.PortraitSwitch setOn:NO animated:YES];
}}

解决方法

试试以下代码

- (IBAction)PortraitSwitchPressed:(UISwitch *)sender
{
        if (sender.isOn)
        {
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
            [self.PortraitSwitch setOn:YES animated:YES];
        }
        else
        {

            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];//or UIInterfaceOrientationLandscapeRight
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
            [self.PortraitSwitch setOn:NO animated:YES];
        }
}

根据您的要求对UIInterfaceOrientation进行修改.

猜你在找的iOS相关文章