2016-04-20 9 views
4

アプリの実行中にアプリの向きを変更する必要があります。私のプロセスは、私がスイッチをオンにすると、アプリはその向きを変更する必要があります(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]; 
}} 

答えて

0

試してみてください。

- (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]; 
     } 
} 

あなたのRごとにUIInterfaceOrientationのための修正を行います等価。

+0

アプリがランドスケープモードになっていて、スイッチをオンにすると、アプリの向きがポートレートモードに変わります。それはいいです。しかし、シナリオ2では失敗します。アプリがポートレートモードのスイッチがONステージにあります。スイッチをオフにしようとすると、向きがランドスケープモードに変わらない – Nithya

+0

'UIInterfaceOrientationLandscapeLeft'または' UIInterfaceOrientationLandscapeRight'を渡してみてください。 –

+0

グレート...ありがとうロナク・チャンヤラ..働いています – Nithya

関連する問題