Not able to have all orientation for iPad - ios

Hi guys i know this question might be asked before as well , so if u could point out right question to refer to that would be great .
Problem - in my info.plist i have only 2 orientation allowed i.e Portrait ,UpsideDown
and every viewController abides to above orientation .
But for one particular viewController i want whole 360 degree orientation .
this viewController is presented modally with style
UIModelPresentationStyle.FullScreen
And to support all orientation in ViewController ,
i have overridden relevant delegate methods as well
ShouldAutorotate is returning -> Yes ,
PreferredOrientationForPresentation is returning -> UIInterfaceOrientation.LandscapeLeft ;
GetSupportedInterfaceOrientation is returning -> UIINterfaceOrientationMask.all
When this ViewController loads i am getting landscapeLeft as expected , when i rotate the iPad, i get portrait mode as well ,
but now i am not able to return back to landscape mode anymore .
Any reason for that???

in my info.plist i have only 2 orientation allowed
Well, that's the problem. You're doing this wrong. The Info.plist should list every orientation that the app can ever have. Your top-level view controllers can then limit their own orientations.

Related

Forcing orientation on a full screen modal doesn't work on parent controller

iOS 16 introduced a new API for forcing orientation changes, which implies using the requestGeometryUpdate(_:errorHandler:).
It seems that it's not working as expected when using a full screen modal.
For example, let's say we have an app that locks the orientation in portrait mode except for a modally presented controller, a parent controller A and a modal controller B :
A is displayed
We put the device in landscape mode - A stays in portrait mode
B is opened modally fullscreen from A
B is displayed in landscape
B has a button to go back to portrait mode using the requestGeometryUpdate(_:errorHandler:)
B is dismissed through a button
A ends up in landscape
In the app delegate, we used supportedInterfaceOrientationsFor to force the app in portait mode except for the controller B.
We tried to call setNeedsUpdateOfSupportedInterfaceOrientations every time we call requestGeometryUpdate(_:errorHandler:).
We also tried to call it on the viewWillAppear from the controller A.
We finally tried to call requestGeometryUpdate(_:errorHandler:) from the controller A's viewWillAppear, without any luck as well.
Is this a bug in iOS 16 or are we missing something ?
It seems to be working as expected when displaying the modal over the current context, but it's not what we want in this case.
I can provide an example project if needed.

Change the device orientation from portrait to landscape on a button click in iOS

I have been trying to change the device orientation on a button click event for 2 days but I am not able to achieve it properly.
So, I have an iPhone app with a minimum version of 12.4, and the device that I am using to test is 14.4. I have used the code mentioned in this project here (which works properly when presenting a new view controller using push or present) but the same doesn't work when on the same view controller. The user needs to rotate his/her device to initiate the orientation change.
This is the code that I have in the view controller that I am trying to rotate on a button click event.
let appDelegate = AppDelegate.sharedInstance
let oldOrientaion = appDelegate.deviceOrientation
appDelegate.deviceOrientation = oldOrientaion == .landscapeRight ? .portrait : .landscapeRight
let value = appDelegate.deviceOrientation.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Can someone please help me in figuring out what exactly is missing from this?
Also, on a side note, the view controller is presented on a different view controller so even if I try to present a new view controller like in the project for some time and change the orientation there it doesn't work.
Edit:
This is fixed now. I was trying to use the UIInterfaceOrientationMask to set the orientation instead of UIInterfaceOrientation. So changing the code to use UIInterfaceOrientation at the end works properly now.
Thanks for the help.
You are doing everything correctly, just need to call the attemptRotationToDeviceOrientation method to the oration change to take effect. Based on the documentation:
Some view controllers may want to use app-specific conditions to
determine what interface orientations are supported. If your view
controller does this, when those conditions change, your app should
call this class method. The system immediately attempts to rotate to
the new orientation.
In your code it will look like:
UIViewController.attemptRotationToDeviceOrientation()

UIImagePicker Controller in Landscape mode

I have used one UIImagePicker Controller in my game. its working fine in iPad Landscape mode using UIPopOver Controller but as UIPopoverController is not supported in iPhone I have used UIIMagePicker Controller for iPhone.
now, it crashes in my iPhone. please help me.
I have select following Orientation in Deplyoment Info of my project :
Landscape Left
Landscape Right
The UIImagePickerController only support's portrait, so you must make sure your app has portrait as a supported orientation. Please see the UIImagePickerController Class Reference documentation, I have included a quote from the document.
IMPORTANT
The UIImagePickerController class supports portrait mode only. This
class is intended to be used as-is and does not support subclassing.
The view hierarchy for this class is private and must not be modified,
with one exception. You can assign a custom view to the
cameraOverlayView property and use that view to present additional
information or manage the interactions between the camera interface
and your code.
EDIT
Look at the UIApplicationDelegate Protocol, it has the following method
func application(application: UIApplication,
supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask
From the documentation:
Discussion
This method returns the total set of interface orientations
supported by the app. When determining whether to rotate a particular
view controller, the orientations returned by this method are
intersected with the orientations supported by the root view
controller or topmost presented view controller. The app and view
controller must agree before the rotation is allowed.
If you do not implement this method, the app uses the values in the
UIInterfaceOrientation key of the app’s Info.plist as the default
interface orientations.

How to create a landscape-view only ios application

I want to create an iOS app that can only work on landscape-view mode.
I Googled how to do that, and end up with supportedInterfaceOrientations method
Here's my attempt:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
I tried to put the above code on viewController.m and AppDelegate.m, but it didn't seem to work
Thanks, any opinion will be much appreciated
You don't need to write any code to run app which supports landscape mode only. Just select app target and uncheck portrait and portrait upside down orientation and make sure landscape orientation is checked.
It seems as though you want to programmatically change the orientation of your application. This can be done by any ViewController by implementing one method.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
That should force the view to auto-rotate in the direction you want it.
When you use -supportedInterfaceOrientationForPresentation it simply states that the returned interfaces can be used, not necessarily that they should be used.
When you use -preferredInterfaceOrientationForPresentation it says that this is orientation that I want, and that the ViewController should switch to it.

iPad 2 UIImagePickerController camera auto-rotation driving me mad!

I've been trying to sort this one out for a while and getting nowhere.
I'm using the camera on the iPad 2 - my application is in landscape, but I want to use the camera in portrait mode. I just can't seem to force the ImagePicker a, into Portrait mode, and b, to stop auto-rotating.
Is there any way that you can force the ImagePicker into portrait (or landscape), and/or to stop it auto-rotating?
Even though the App is in landscape and is set to return YES only to landscape orientations, the ImagePicker ignores that and rotates into portrait when you rotate the iPad 2.
If I could force it into staying in landscape then (although not ideal) I could re-design the app to use the camera in landscape, but I don't seem to be able to do even that!
Any help greatly appreciated!
The method that you need to override called:
_isSupportedInterfaceOrientation:
So it should look something like this :
#interface PortraitImagePickerController : UIImagePickerController {
}
#end
#implementation PortraitImagePickerController
- (BOOL)_isSupportedInterfaceOrientation:(UIDeviceOrientation)orientation{
return UIDeviceOrientationIsPortrait(orientation);
}
#end
But it's definitely private method so Apple may reject your app
Well i have found a more convenient way and has no risk of using private code.
Sub class a UIImagePickerController and add following method in its .m file.
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

Resources