问题
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.
回答1:
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, e.g. 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.
回答2:
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);
来源:https://stackoverflow.com/questions/44305964/programmatically-rotate-ios-app-upside-down-using-objective-c