如何根据开关按钮的开和关来更改设备的方向?

尼西亚

我需要在应用运行时更改应用方向。我的过程是,如果我打开开关,则应用程序应更改其方向(即)(i)如果应用程序以横向方向运行,则如果我将开关按钮“ ON”,则设备方向应自动更改为纵向模式。 。(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];
}}
Ronak chaniyara

试试下面的代码:

- (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根据您的要求进行修改

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章