Locking orientation issue in iphone - ios

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.

Related

iOS 11 Device Orientation Autorotation

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
}

Forcing Portrait Orientation if iOS

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.
}

Presented UIViewController refuses to lay out with landscape orientation

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);
}

Landscape for only certain devices in iOS 8.0

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.

Why is my ModalView displaying in portrait mode?

Hopefully somebody can point out what I'm doing wrong with my Splash screen here. The problem is that the screen is displaying in portrait mode, not landscape...
- (void)showSplash
{
UIView *modelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024 , 748)];
UIViewController *modalViewController = [[UIViewController alloc] init];
[modelView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Splash.png"]]];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:#selector(hideSplash) withObject:nil afterDelay:5.0];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Thanks in advance for the help.
Looks like you are writing app for iPad. If so, you have to support both landscape as well as portrait orientation otherwise Apple will reject it. I would suggest that you should use two different images. Image specifications are as follows:
Default-Landscape.png (1004 * 768)
Default-Portrait.png (748*1024)
(I am assuming that you are showing status bar if not add 20 pixels to height of an image)
That's it, create these images and add it to your project. And you are good to go. No Need to write any additional piece of code too..
And ya make it
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You shouldn't rely on a function in your code to display the splash screen. Just define them as the previous answer from Sumit Lonkar explains.
If you do it in code, I believe at the start of the application the orientation is always considered as portrait, then the transition to the actual orientation is triggered. This explains why your code displays first as portrait and most likely there is nothing else in the code to handle rotation. Besides, the purpose of the splash screen is to display something while the app is loading, so if you put it in code you lose the purpose.
By doing it the Apple way you leave it to another Apple process that runs before looking at your code and it will work.
Regarding the orientation supported I have on my iPad some apps that support only landscape (TapZoo for example) so it should be ok with Apple.

Resources