Note that there are some explanatory texts on larger screens.

plurals
  1. POForce device rotation with +attemptRotationToDeviceOrientation failing
    primarykey
    data
    text
    <p>According to the <code>UIViewController</code> docs here, </p> <p><a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation" rel="noreferrer">https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation</a></p> <p>using <code>[UIViewController attemptRotationToDeviceOrientation]</code> forces the system to attempt to rotate the interface to a new orientation. I am attempting to force an interface rotation from portrait to landscape by:</p> <ul> <li>setting a <code>forceLandscape</code> flag before attempting the rotation</li> <li>Calling <code>[UIViewController attemptRotationToDeviceOrientation]</code></li> <li>implementing <code>-supportedInterfaceOrientations</code> in my view controller and checking a flag to change the supported orientations, e.g.</li> </ul> <p>This forces <code>-supportedInterfaceOrientations</code> to be called again, which looks like</p> <pre><code>- (NSUInteger)supportedInterfaceOrientations { return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait; } </code></pre> <p>Even though <code>-supportedInterfaceOrientations</code> is being called and returns the new orientation, the interface does not rotate. I have confirmed that the project allows all orientations and that this view controller is the window's root view controller. Has anyone else run into this problem and found a solution? I am aware of all the hacks to fix this (presenting and dismissing modals, etc), but I would like a clean solution if possible. I have verified this issue on iOS 6 and 7.</p> <p>Below is the complete code to reproduce:</p> <pre><code>@interface ALTViewController () @property (nonatomic, assign) BOOL forceLandscape; @end @implementation ALTViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.forceLandscape = NO; } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"button" forState:UIControlStateNormal]; [button sizeToFit]; [self.view addSubview:button]; button.center = self.view.center; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; } - (void)buttonPressed:(id)sender { self.forceLandscape = YES; [UIViewController attemptRotationToDeviceOrientation]; } - (NSUInteger)supportedInterfaceOrientations { return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload