Understanding iOS6 autorotation in more depth - ios

I have read multiple QAs on this topic and read the documentation, but I would like some confirmation on the answers.
- (BOOL)shouldAutorotate
This method determines if the VC is allowed to autorotate?
-(NSUInteger)supportedInterfaceOrientations
This method determines what orientations the specific View Controller accepts, is able to rotate too...
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
This method determines what the preferred orientation is for. However, it is not called on the VC but on a navigation controller. So if you wish to use you should subclass uinavigationcontroller.
Is this all correct?
What I am trying to do is have one specific controller auto rotate to landscape if an image is passed to it in that orientation. I can get the image orientation without an issue. I have set the shouldAutorotate to YES on that VC, and the supportedInterfaceOrientations is set to all. However it does not rotate.
This is from a UINavigationController that modally presents another UINavigationController on top of it and is the specific controller is about 6th in the stack.
How do I control the preferred orientation for a VC within a UINavigationController which is determined by a value from that View Controller? As the orientation would not always be the same.

Be careful you don't have orientation lock on the device whilst testing!!! Opps

Related

Stop autorotation of UIViewController at creation time

Is there any way to set autorotate behavior of an UIViewController object when it's initiated?
I wanted it to never rotate, so I tried this:
UIViewController *myViewController = [[UIViewController alloc]init];
myViewController.shouldAutorotate = NO;
Seemed logical, but it doesn't work. Get the error message:
No setter method 'setShouldAutorotate:' for assignment to property
Or is it possible only through subclassing?
You should this way:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Return Value
A bit mask specifying which orientations are supported.
See UIInterfaceOrientationMask for valid bit-mask values. The value
returned by this method must not be 0.
Discussion
When the user changes the device orientation, the system
calls this method on the root view controller or the topmost presented
view controller that fills the window. If the view controller supports
the new orientation, the window and view controller are rotated to the
new orientation. This method is only called if the view controller's
shouldAutorotate method returns YES.
Override this method to report all of the orientations that the view
controller supports. The default values for a view controller's
supported interface orientations is set to
UIInterfaceOrientationMaskAll for the iPad idiom and
UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.
The system intersects the view controller's supported orientations
with the app's supported orientations (as determined by the Info.plist
file or the app delegate's
application:supportedInterfaceOrientationsForWindow: method) to
determine whether to rotate.
Source: UIViewController on iOS Developer Library.

iOS8 - prevent rotation on presenting viewController

We have a MainViewController with a tableView, and it presents a new modalViewController.
The MainViewController is restricted to portrait only, and the modalViewController can rotate.
The problem is in iOS8, that when the modalViewController rotates, the callback method of rotation in iOS8 in MainViewcontroller is called - - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
Thus, the UITableView is getting its data reloaded, which is a behaviour we don't want.
Can we prevent this feature of iOS 8, and not rotate the presenting UIViewController?
So after long days of searching and investigating, I finally came up with a possible solution.
First of all, I can use navigation controller and push the viewController instead of presenting it, but it breaks my code and just isn't so true.
The second thing I can do is not setting constraints. I still can use autolayout, but if I don't set constraints, and let the default constraints to be set, the tableView doesn't get reloaded. of course this is also isn't very smart thing to do, as I have many elements in my viewController.
Finally, I figured out that I can show this "modal" viewController in another UIWindow. I create UIWindow and set the modalViewController as its rootViewController.
I put some example project in git:
https://github.com/OrenRosen/ModalInWindow
Hope it will be helpful.
I did something similar with a navigation controller, that wouldn't rotate unless the top pushed controller does rotate.
In your case check if the main controller is presenting another controller. If it isn't then just reject rotation, otherwise return whatever the presented controller returns for the rotation method.
As for your table view, it shouldn't get reloaded because of rotations.
In iOS 8 the view that rotates when you change the device orientation is the first view added to the UIWindow. So, if you save a reference to it in your presentedController, you can overwrite the shouldAutorotate and supportedInterfaceOrientations values.

How to disable autorotation in iOS 8 for a particular controller?

-(BOOL)shouldAutorotate {
return NO;
}
Above method works for one controller but when there are multiple viewControllers pushed on a stack.
I want a particular controller that should be displayed in portrait mode only.
- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
}
I have used above method suggested on stack overflow for iOS 8 but it does not give desired result.
First, use -supportedInterfaceOrientations instead of -shouldAutorotate. -shouldAutorotate should only be used when you must disallow autorotation based on factors determined at runtime. You know your view controller will always only support portrait mode, there is no runtime decision here.
Next, your navigation controller's delegate must implement the -navigationControllerSupportedInterfaceOrientations: method to return the result of calling -supportedInterfaceOrientations on the view controller at the top of the navigation stack.
-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
return navigationController.topViewController.supportedInterfaceOrientations;
}
An important caveat: A view controller pushed onto a navigation stack has no control over its initial interface orientation; that will always be the current interface orientation. What the above technique will do is prevent the interface from rotating to any orientation other than portrait while that view controller is displayed.

iOS Orientation Support both Landscape and Portrait on specific screen - NOT ALL Screens

Comrades,
I just would like to know, how to enable Landscape Orientation only on Specific screens? As of now, I selected Landscape Left and Right options in General Settings and enabled in Supported interface orientations (iPhone) in plist file for the device Orientation, but that impacts all the screens.
I have nearly 80 screens in my application, I need to support both Portrait and Landscape about 5 screens, rest of the screens should be shown only in Portrait mode.
Any help is greatly appreciated!
Thanks,
Ramesh
In General Settings (which just adjusts your plist), you need to select all possible supported orientations. Then, you need to limit them in your specific view controller. If you're using a NavBar or TabBar controller, you need add your limitation there.
From the UIViewController docs:
In iOS 6 and later, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported. The intersection of the app’s orientation mask and the view controller’s orientation mask is used to determine which orientations a view controller can be rotated into.
To make this simpler, I created a category on UINavigationController that looks at the top-most view controller to determine it's rotation abilities. That way, in my specific view controllers that needed rotating, I could override those same methods and add landscape support.
#implementation UINavigationController (AutoRotation)
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
#end
Are your screens inside a UINavigationController? If so, I've noticed not all viewControllers can decide what orientations they support.

How to configure SWRevealViewController/PKRevealController/etc to handle orientation and presentation changes

I am using SWRevealViewController (https://github.com/John-Lluch/SWRevealViewController) to handle switching between two controllers, a "front" and a "rear".
The front controller is a UINavigationController and the rear controller is just a plain UIViewController that displays a list of menu items. The front UINavigationController pushes an instance of a view controller named FrontViewController. The rear UIViewController is an instance of RearViewController. The instance of SWRevealViewController is set as the root view controller once it is configured with the front and rear controllers, the delegate property of the reveal controller is set to the app delegate itself.
In both FrontViewController and RearViewController I am overriding shouldAutorotate and returning NO as well as overriding supportedInterfaceOrientations and returning UIInterfaceOrientationMaskPortrait.
However the app auto rotates and goes into Landscape while these two views are displaying when I rotate the device.
supportedInterfaceOrientations seems to only be invoked in FrontViewController but the value is not honored and the device rotates into landscape orientation.
I can't simply set the entire app to Portrait either because I have other detail views that I do want to support Landscape (movie player, etc).
How can I get SWRevealViewController working so I can restrict the app to Portrait in certain child views of the controller?
I have also noticed that the presentation changes are not being honored as well. In RearViewController I am overriding prefersStatusBarHidden to return YES but this method is never invoked. Similarly in FrontViewController I am overriding preferredStatusBarStyle to return UIStatusBarStyleLightContent but this method is never called either.
I have UIViewControllerBasedStatusBarAppearance set to YES in my plist.
UPDATE:
I have tried to use PKRevealController as suggested in the comments but the behavior is exactly the same. Supported orientations and status bar styles are completely ignore. supportedInterfaceOrientations on FrontViewController is the only override invoked but the return value of UIInterfaceOrientationMaskPortrait is not honored.
UPDATE 2:
I'm thinking that this is simply a limitation of these controls and they pretty much expect the support orientations to be the same throughout the application. I did however try MFSideMenu (https://github.com/mikefrederick/MFSideMenu) and it seems to handle supported orientations in different child views exactly as you would expect it to. I still don't have the status bar visibility and styles working, unfortunately.
To achieve this while not avoiding SWRevealViewController: Inside of it there is implemented method supportedInterfaceOrientations, which needs to be edited. I have done this:
- (NSUInteger)supportedInterfaceOrientations
{
UINavigationController* frontNavigationController = (UINavigationController*)self.frontViewController;
if ([frontNavigationController.visibleViewController isKindOfClass:[VCgallery class]]) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}
All my viewControllers are portrait only except those that are of class VCgallery, which can be both portrait and lanscape.
The solution ended up being to subclass PKRevealController and override supportedInterfaceOrientations, shouldAutorotate, preferredInterfaceOrientationForPresentation, prefersStatusBarHidden, and preferredStatusBarStyle.
While MFSideMenu did this for orientation it did not support status bar configurations. I also ran into a major bug with MFSideMenu that prevented me from using it in my project.
I made each method I override return a value from the appropriate controller depending on the circumstance. In the case orientation I return the value from self.frontViewController.topViewController (since I am using a UINavigation controller). Status bar style and visibility came from either self.frontViewController or self.leftViewController depending on the current state.
The same solution probably would have worked for SWRevealViewController as well but I preferred the API design of PKRevealController.
I figured subclassing would work from the beginning but I assumed that such a common scenario would be handled in the configuration of these controls.

Resources