Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display one UIView in landscape?

I've looked at every question so far and none seem to actually answer this question.

I created a UITabBarController and added several view controllers to it. Most of the views are viewed in portrait, but one should be viewed in landscape. I don't want to use the accelerometer or detect when the user rotates the device, I just want to display the view in landscape mode when they choose that view from the tab at the bottom.

I want the regular animations to occur, such as the tab dropping out, the view rotating, etc., when they choose that item, and the opposite to happen when they choose a different view.

Is there not a built-in property or method to tell the system what orientation to display the view as?

Overriding the shouldautorotate... method does absolutely nothing so far as I can tell.

The type of answer I would NOT appreciate is "RTFM" because I already have, and anybody who's developed for the iPhone so far knows that there is very little useful M to F-ing R.

like image 925
Ed Marty Avatar asked Dec 10 '08 17:12

Ed Marty


2 Answers

An post on a forum that might help. Short answer is you have to manually rotate your view or controller once the view has been drawn, in the viewWillAppear: method

CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);

[[appDelegate navController].view setTransform:landscapeTransform];
like image 162
JamesSugrue Avatar answered Oct 23 '22 01:10

JamesSugrue


Here's what I'm doing to do this:

first, put this define at the top of your file, right under your #imports:

#define degreesToRadian(x) (M_PI * (x) / 180.0)

then, in the viewWillAppear: method

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}

if you want that to be animated, then you can wrap the whole thing in an animation block, like so:

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];
like image 23
Bdebeez Avatar answered Oct 23 '22 01:10

Bdebeez