简体   繁体   中英

Programmatically rotate iOS app upside down using objective-c

I want to rotate my app upside down portrait using the following code in the ViewController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    //return NO;
  // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationMaskPortraitUpsideDown);
}


- (UIInterfaceOrientationMask) supportedInterfaceOrientations

{
  return [super supportedInterfaceOrientations] | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
}

In the AppDelegate:

- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{
  return UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
}

When the user presses a button

NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
  [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

The app does somehow not rotate and I have no idea why. Somebody can give a hint? Thank you I added the orientations in the info.plist and set the checkboxes in the general settings in deployment info section.

Do I understand you correctly, that you want to programatically want to rotate the user interface, independend from the physical orientation of the device?

This is not how it works: The delegate methods (shouldAutorotate... etc.) are called by iOS when a (physical) rotation of the device is detected, eg the user turns it upside down. In the delegate methods you then get the chance to adopt the views to the new orientation, if you need to do so.

In the simulator, you can simulate device rotation by Alt+Left / Alt+Right keys.

You cannot rotate the orientation programatically it depends on sensors of the device. However you can rotate the layer of the view.

self.view.layer.transform = CGAffineTransformMakeRotation(M_PI_2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM