Thomas
Thomas

Reputation: 6188

How do I rotate from landscape to portrait upon loading a view?

I'm working on a module for a project where the screen is in landscape mode when I get it. I have to load a new view, and switch to portrait mode. While this seems simple enough, I have not been able to find a simple solution. Here's what I tried:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
locationsView =[[LocationsViewController alloc] initWithNibName:@"LocationsViewController" bundle:nil];
[self.view addSubview:locationsView.view];

and that did not work like I needed. It rotated the status bar to portrait mode, but not the rest of the view.

Any suggestions?

Thomas

Upvotes: 1

Views: 1244

Answers (1)

Thomas
Thomas

Reputation: 6188

This did it:

[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
    //resize the view
self.view.bounds = CGRectMake(0.0, 0.0, 320, 460);
self.view.center = CGPointMake(240, 140);

locationsView =[[LocationsViewController alloc] initWithNibName:@"LocationsViewController" bundle:nil];
[self.view addSubview:locationsView.view];

And make sure degreesToRadian is defined in global.h.

Upvotes: 1

Related Questions