I've made a puzzle game ('WordBatch', available in the App store) and thought I had forced portrait orientation but its still rotating to landscape mode, though only the iPad. In XCode I specified it as a 'Universal' App, for all devices, and checked portrait mode only in the General tab. Even in the plist file portrait is the only orientation specified. I've tried all the suggestions in StackOverflow on this subject, but to no avail. The current code I have on place is this bit in my AppDelegate:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
and the following is in my root view controller:
-(BOOL)shouldAutorotate {
return NO;
}
but it still rotates to landscape. Any help here would be great.
You can try by applying this check:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad.
}
else
{
// The device is an iPhone or iPod.
}
Related
For iOS 10 and below, the following code controlled the orientation of the any respective UIViewController. I have selected Portrait, Landscape Left, and Landscape Right in my Deployment Info, and have the following in my Info.plist:
For my VC's that should not rotate at all I have the following code, which I stated, was working prior iOS 11
- (BOOL)shouldAutorotate {
[super shouldAutorotate];
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
[super supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
I have tested this on actual devices and as of iOS 11 it does not work.
Even more strangely, logging the registered device orientation as of iOS 11 tells my the device IS portrait... when the controller loads in landscape mode...
Code:
// In viewDidLoad
NSLog(#"orientation: %lu", [[UIDevice currentDevice] orientation]);
Console output:
2017-09-22 15:20:26.225196-0400 <APP_NAME>[2669:1628408] orientation: 1
This occurs either rotating the device left or right before building and running the app.
What is the cause here for this error? If anyone knows please help..
FYI: No, I do not have rotation lock on my device..
Whether this proves to be an iOS 11 bug or not, I seemed to have stumbled upon a "solution" to this issue. For whatever reason, for iOS 11 changing the - (BOOL)shouldAutorotate return to YES allows for the correct orientation...
Objc
- (BOOL)shouldAutorotate {
[super shouldAutorotate];
if (#available(iOS 11, *)) return YES;
return NO;
}
In combination I had to do a manual check for the screen dimensions to see if the width was greater or less than the supposed height of the screen.
width = self.view.frame.size.width, height = self.view.frame.size.height;
if (height < width) width = height, height = self.view.frame.size.width;
Hopefully someone else finds the true cause of this "bug" OR Apple updates their bundle to handle rotations like all previous iOS versions..
Swift 3.2+
override var shouldAutorotate: Bool {
if #available(iOS 11.0, *) {
// Anything else iOS 11 specific
return true
}
return false
}
I am updating a 5-year-old app (originally written for iOS 3!). I have made decent inroads in using autolayout and addressing deprecation warnings. But the old technique used for presenting a different view controller when the device is rotated no longer works reliably.
- (void)orientationChanged:(NSNotification *)notification {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIInterfaceOrientationLandscapeRight && !showingOtherVC) {
// switch to other VC
othervc.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:othervc animated:YES completion:nil];
[self resignFirstResponder];
}
The other view controller does appear, but it's laid out sideways, for a portrait screen, not landscape, even though the device is in a landscape orientation.
How can I update this in a reasonably easy way (i.e., not a rewrite in Swift, not restructuring the app with storyboards — which Xcode doesn't seem to facilitate via copy/paste)? And, for the benefit of others who may happen on this question, what would be the more correct way to achieve this result (new VC on orientation change) if I were writing this from scratch?
Thank you.
This was a really stupid error, but in case someone else makes it and ends up here, this was the problem.
Instead of correctly returning the mask constant:
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskLandscapeRight);
}
I was returning this other constant that autocomplete gave me:
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationLandscapeRight);
}
I have a structure like this
LoginViewController-->Root
LoginViewController-->UINav---->HomeViewController
Now I have to lock this orientation to portrait only for iPhone and have to provide both orientations for iPad
For the Login Controller, I wrote this
-(BOOL)shouldAutorotate{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
return YES;
}
else
{
return NO;
}
}
This is working fine. I got only a portrait orientation in iPhone and both portrait as well as landscape orientation in iPad
But the same piece of code is written in HomeViewController is not working.
Is it due to the Navigation Controller which is embedded with HomeViewConroller.
Well. I got a fix for that myself. Posting the solution on what to do as it might help others too.
Just uncheck the Device Orientation checks (LandscapeLeft and LandscapeRight) in Target->General
And write the same piece of code in every controller
-(BOOL)shouldAutorotate{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
return YES;
}
else
{
return NO;
}
}
This worked for me. Now I only get a portrait mode in iPhone and a landscape as well as a portrait mode in iPad.
With iOS 8.0 in Xcode 6, there is nowhere where I can see that you can specify for each device if you want it portrait or landscape...
I want to have all iPhones only portrait except 6 Plus which should be both portrait and landscape. And I want all iPads to be both portrait and landscape.
Is there a way to do this or even a workaround it?
To make the 6+ different than other iPhones you'll need to implement supportedInterfaceOrientations something like this:
- (NSUInteger)supportedInterfaceOrientations
{
if (iPadOrPhonePlus()) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
I haven't settled on a way I like for that helper method but you can start with this:
.#define iPadOrPhonePlus() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || [[UIScreen mainScreen] scale] > 2)
ignore the . before the #define SO was formatting it strangely without that.
Suppose I want to give permission to the user if he wants to rotate in iPad.
There is no rotation scheme for iPhone so I need not provide this option of iPhone settings.
To check if the device is iPad, you can do this:
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
//Setting for iPad
}
else {
//Setting for iPhone
}
Similarly you can also do this:
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
//Setting for iPhone
}
else {
//Setting for iPad
}
BOOL isiPad;
//Declare global Bool value
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// The device is an iPad
// Enable rotation here
isiPad=YES;
}
else {
// The device is an iPhone or iPod touch.
// disable rotation here
}
//Set your rotation methods here
-(BOOL)shouldAutorotate
{
return isiPad;
}
In your project
Target->Summery-> Supported Interface Orientations
in that it gives settings for interface orientations for iPhone and i pad separately
Check it once
Like below image
Settings.bundle file is common in program. Its not like that if you an Universal application than you can have separate file for both iPhone & iPad. It will be 1 common file.
You should give validations by checking If iPad Device than you can open orientations or if iPhone Device & lock the Orientation.