Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

My app is only for iphone device (both iphone 4 and 5) and built to support only ios 6.

My whole app only supports portrait mode. But there is one view called "ChatView" , which i want to support both landscape and portrait modes.

I have set the required device rotations as follows -

enter image description here

I have also tried following code to support rotation in "ChatView" -

-(BOOL)shouldAutorotate {     return YES; }  -(NSUInteger)supportedInterfaceOrientations {     return UIInterfaceOrientationMaskLandscape; } 

But it could not rotate that view.

I have searched a lot for this , but could not be able to find the solution for my issue.

And also in "ChatView" there are some objects like buttons, textfields whose frames are set programmaticaly. So i want to know should i have to set frames of all those objects for landscape mode also?

Please help me.

Thanks.....

like image 761
Rohan Avatar asked Jul 04 '13 08:07

Rohan


People also ask

How do I rotate a single ViewController to landscape?

Use the shouldAutorotate and the supportedInterfaceOrientations method in the ViewController you want to display in landscape and portrait mode: This method should override the storyboard-settings. Hey Christian, this answer only seems to work if you subclass the UINavigationController and return self.

How do you lock rotation on iPhone Swift?

Go it the main and select TARGETS then select Info tab (the plist) and open Supported inferface orientations (iPhone) the click on the ones that you do not need. Just leave Portrait(bottom home button) . That should make the UI stay one way.

How do you change to portrait mode in Swift?

Set the supportedInterfaceOrientations property of specific UIViewControllers like this: class MyViewController: UIViewController { var orientations = UIInterfaceOrientationMask. portrait //or what orientation you want override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.


1 Answers

Simple but it work very fine. IOS 7.1 and 8

AppDelegate.h

@property () BOOL restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if(self.restrictRotation)     return UIInterfaceOrientationMaskPortrait; else     return UIInterfaceOrientationMaskAll; } 

ViewController

-(void) restrictRotation:(BOOL) restriction {     AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;     appDelegate.restrictRotation = restriction; } 

viewDidLoad

[self restrictRotation:YES]; or NO 
like image 70
Alan10977 Avatar answered Sep 28 '22 01:09

Alan10977