Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support different orientation for only one view iOS 6

I want to rotate ONLY one of my views within my app to either landscape left or landscape right. All my other views are in portrait mode and I have set my app to support only portrait mode. With orientation being changed in iOS 6, I am not sure how to do this. I have tried the following posted below. Can anyone tell me what I am doing wrong? Thanks!

-(BOOL)shouldAutorotate { return YES; }  -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; }  -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; }  

I have also tried:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self                                          selector:@selector(didRotate:)                                             name:UIDeviceOrientationDidChangeNotification                                            object:nil]; return YES;//UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; }     -(void)didRotate:(NSNotification *)notification {  UIDeviceOrientation orientation = [[notification object] orientation];  if (orientation == UIDeviceOrientationLandscapeLeft) {     [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];     [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; } else if (orientation == UIDeviceOrientationLandscapeRight) {     [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];     [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {     [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];     [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; } else if (orientation == UIDeviceOrientationPortrait) {     [theImage setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];     [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; } } 
like image 769
Alex G Avatar asked Oct 07 '12 20:10

Alex G


People also ask

How do I force horizontal orientation in IOS?

On an iPhone with a Home button, swipe up from the bottom of the screen to access it. On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.

How do I force landscape in Swift?

Setting the app upWhen on the main screen, under the orientation section, you will see a number of options like 'Auto-rotate OFF', 'Auto-rotate ON', 'Forced Portrait' and 'Forced Landscape'. As the names suggest, you can use these buttons as one-tap shortcuts to toggle the orientation of your device.


2 Answers

This worked for me How to force a UIViewController to Portrait orientation in iOS 6

Create a new category from UINavigationController overriding the rotating methods:

-(BOOL)shouldAutorotate {     return [self.topViewController shouldAutorotate]; }  -(NSUInteger)supportedInterfaceOrientations {     return [self.topViewController supportedInterfaceOrientations]; }  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {     return [self.topViewController preferredInterfaceOrientationForPresentation]; }  @end  
like image 124
ChavirA Avatar answered Sep 25 '22 01:09

ChavirA


There are changes in iOS 6 regarding handling view rotations. Only orientations defined in apps Info.plist are supported. Even if you are returning other ones.

Try to select all orientations as supported in your project.

Handling View Rotations

In iOS 6, 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. Generally, 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 in 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.

You can override the preferredInterfaceOrientationForPresentation for a view controller that is intended to be presented full screen in a specific orientation.

In iOS 5 and earlier, the UIViewController class displays views in portrait mode only. To support additional orientations, you must override the shouldAutorotateToInterfaceOrientation: method and return YES for any orientations your subclass supports. If the autoresizing properties of your views are configured correctly, that may be all you have to do. However, the UIViewController class provides additional hooks for you to implement additional behaviors as needed. Generally, if your view controller is intended to be used as a child view controller, it should support all interface orientations.

When a rotation occurs for a visible view controller, the willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are called during the rotation. The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.

(C) Apple Docs: UIViewController

like image 44
Nekto Avatar answered Sep 22 '22 01:09

Nekto