Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-31-2008, 12:37 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 16
Default Force landscape mode in one view

Hello,

How do you force a view to be in landscape mode? I've tried the following code (which generates a warning), but it isn't working for me (in the view controller):

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
		[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
	}
	return self;
}
Thanks,
Justin
Opsive is offline   Reply With Quote
Old 08-31-2008, 07:55 AM   #2 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
Send a message via Skype™ to Jume
Default

Use the search function. There's already several topics up here which deal with landscape mode...
Jume is offline   Reply With Quote
Old 09-03-2008, 12:57 PM   #3 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Quote:
Originally Posted by Jume View Post
Use the search function. There's already several topics up here which deal with landscape mode...
i have searched throughout multiple forums and have found lots of information about an entire application in landscape, but almost nothing about just one view displaying in landscape orientation. I have my application displaying in portrait and want one view to display in landscape. Could you please show me where this is discussed?

Thanks.
cope784 is offline   Reply With Quote
Old 09-03-2008, 01:30 PM   #4 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
Send a message via Skype™ to Jume
Default

one two three
Jume is offline   Reply With Quote
Old 09-04-2008, 08:19 AM   #5 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Quote:
Originally Posted by Jume View Post
Jume,

thanks for the fast response. I appreciate the examples and they are helping me to understand. We were able to start the "Window" and "tableview" in landscape but the NavigationBar is not transforming. Here is a snapshot of our code:

Code:
- (void)loadView {
	
	// create and configure the table view
	myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.autoresizesSubviews = YES;
	myTableView.scrollEnabled = YES;
	// BWC: allows the TableView to resize in landscape or portrait orientation
	
	myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	
	// this will transform your coordinates so that you can draw kind of normally
	// Set up to rotate 90 degrees (PI/2 radians)
	CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57079633);
	[myTableView setTransform:rotate];
	self.view = myTableView;

}
Please let me know if you think there is something we are not doing correctly. One thing that may be the problem is the fact we are setting up a UIView.

Thanks,
Brad

Last edited by cope784; 09-30-2008 at 11:08 AM.
cope784 is offline   Reply With Quote
Old 09-04-2008, 12:37 PM   #6 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
Send a message via Skype™ to Jume
Default

I think you should apply transformation to every UI element that you want to render in landscape...

But I don't quite get it. If you have a navigation bar that means that you have a navigation controller as well? Why don't you use the autorotate functionality that view controllers serve to you? It's much easier and the whole interface is rotatet for you. Just need to return YES shouldRotateToInterfaceOrientation ...

Cheers
Jume is offline   Reply With Quote
Old 09-04-2008, 12:51 PM   #7 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Quote:
Originally Posted by Jume View Post
I think you should apply transformation to every UI element that you want to render in landscape...

But I don't quite get it. If you have a navigation bar that means that you have a navigation controller as well? Why don't you use the autorotate functionality that view controllers serve to you? It's much easier and the whole interface is rotatet for you. Just need to return YES shouldRotateToInterfaceOrientation ...

Cheers
That is true. We are using a navigation controller and have tried to use the shouldAutorotateToInterfaceOrientation function. Here is the code we are using for that function.

Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
As far as I understand it, that should allow the phone to autorotate to Landscape Right when rotated on its side. The only problem is that the view first loads in portrait and we want it load and be viewed only in landscape right. We are basically trying to show tableview in landscape mode because the information that will be displayed is easier to read in landscape than in portrait.

Sorry for the confusion but hopefully this will help explain what we are trying to do.

Again, thanks for your time trying to help me.

Last edited by cope784; 09-30-2008 at 11:08 AM.
cope784 is offline   Reply With Quote
Old 09-04-2008, 02:57 PM   #8 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
Send a message via Skype™ to Jume
Default

Yeah I understand. I have simmilar situation in my app. Then the only way to go is to apply rotation transformation manually to all the views. I'm doing it so. I haven't seen other methods for doing that.

Maybe there is one thing you could try. It might work:
- create a single UIView that will be uses as a wrapper for your UINavigationController (and it's subviews)
- add this UINavigationController as a subview to that view
- apply rotation transformation on that wrapper view. that should apply the rotation to all it's subviews. At list I think it should.

and ofcourse report back if it's working

Last edited by Jume; 09-04-2008 at 03:00 PM.
Jume is offline   Reply With Quote
Old 09-05-2008, 07:34 AM   #9 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Thanks for the advice. I will give it a try and let you know how it goes.
cope784 is offline   Reply With Quote
Old 09-09-2008, 08:09 AM   #10 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Quote:
Originally Posted by Jume View Post
Yeah I understand. I have simmilar situation in my app. Then the only way to go is to apply rotation transformation manually to all the views. I'm doing it so. I haven't seen other methods for doing that.

Maybe there is one thing you could try. It might work:
- create a single UIView that will be uses as a wrapper for your UINavigationController (and it's subviews)
- add this UINavigationController as a subview to that view
- apply rotation transformation on that wrapper view. that should apply the rotation to all it's subviews. At list I think it should.

and ofcourse report back if it's working
I tried that and it was unsuccessful. I will figure it out. Thanks again for the help and advice.
cope784 is offline   Reply With Quote
Old 09-30-2008, 11:14 AM   #11 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Jume,

We were able to get working. Here is what we did:

First in the interface (this is only partial b/c of company policies i can't show you everything):
Code:
@interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
	UITableView	*myTableView;
	CGAffineTransform _originalTransform;
	CGRect _originalBounds;
	CGPoint _originalCenter;
}

@property (nonatomic, retain) UITableView *myTableView;

@end
Next here is the implementation:
Code:
@implementation YourViewController

@synthesize myTableView;

- (void)loadView {
	
	UIView *viewContainer = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

	CGRect tableFrame;
	tableFrame.origin.x += 10;
	tableFrame.origin.y -= 15;
	tableFrame.size.height = 320;
	tableFrame.size.width = 460;
	
	// create and configure the table view
	myTableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
	
	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.autoresizesSubviews = YES;
	myTableView.scrollEnabled = YES;	
	myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

	
	[viewContainer addSubview:myTableView];
	self.view = viewContainer;	
}

- (void)viewWillAppear:(BOOL)animated {
	
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	
	_originalTransform = [[appDelegate navController].view transform];
	_originalBounds = [[appDelegate navController].view bounds];
	_originalCenter = [[appDelegate navController].view center];
	
	CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
	landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);
	
	[[appDelegate navController].view setTransform:landscapeTransform];
	
	[appDelegate navController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	[appDelegate navController].view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
	[appDelegate navController].view.center  = CGPointMake (240.0, 160.0);
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}

- (void) viewWillDisappear:(BOOL)animated {
	
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	[[appDelegate navController].view setTransform:_originalTransform];
	[[appDelegate navController].view setBounds:_originalBounds];
	[[appDelegate navController].view setCenter:_originalCenter];
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}
As you can see we are manually transforming the views and restoring the correct views. There is one problem interface bug when you leave this view, but nothing major.

Last edited by cope784; 10-09-2008 at 03:22 PM.
cope784 is offline   Reply With Quote
Old 10-03-2008, 02:54 AM   #12 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
Send a message via Skype™ to Jume
Default

Thanks for posting your code. I'm just at it and trying to apply it to my code.
Maybe I'm back with more questions.

Thanks,
Jume
Jume is offline   Reply With Quote
Old 10-03-2008, 08:37 AM   #13 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Can you explain this comment:

Quote:
// no scrolling in this case, we don't want to interfere with touch events on edit fields
PhoneyDeveloper is offline   Reply With Quote
Old 10-09-2008, 03:23 PM   #14 (permalink)
New Member
 
Join Date: Jun 2008
Posts: 22
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
Can you explain this comment:
Phoney, sorry for the confusion. That was an unnecessary and leftover code. I have removed it from the code. thanks for the question.
cope784 is offline   Reply With Quote
Old 12-15-2008, 09:33 AM   #15 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 1
Default problem with UINavigationBar

So I'm having a strange problem. I'm doing something similar to what you're doing, but I hide the UINavigationBar after a delay. However, when the user brings it back (via a click), it comes back sized for portrait mode. Any thoughts as to how to force the UINavigationController to redraw the bar correctly (note that the first time it draws after the transformation, it draws correctly).

thanks in advance, Chuck

Quote:
Originally Posted by cope784 View Post
Code:
- (void)viewWillAppear:(BOOL)animated {
	
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	
	_originalTransform = [[appDelegate navController].view transform];
	_originalBounds = [[appDelegate navController].view bounds];
	_originalCenter = [[appDelegate navController].view center];
	
	CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
	landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);
	
	[[appDelegate navController].view setTransform:landscapeTransform];
	
	[appDelegate navController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	[appDelegate navController].view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
	[appDelegate navController].view.center  = CGPointMake (240.0, 160.0);
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}

- (void) viewWillDisappear:(BOOL)animated {
	
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	[[appDelegate navController].view setTransform:_originalTransform];
	[[appDelegate navController].view setBounds:_originalBounds];
	[[appDelegate navController].view setCenter:_originalCenter];
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}
DrChuck is offline   Reply With Quote
Old 12-15-2008, 04:19 PM   #16 (permalink)
Twitter: fabiankr
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

First it draws in portrait, too, but gets rotated.
You will have to rotate the navigation bar after it got reactivated.

Your methods "viewWillAppear" and "viewWillDisappear" won't be called.

Last edited by Forsworn; 12-15-2008 at 04:25 PM.
Forsworn is offline   Reply With Quote
Old 12-16-2008, 07:01 AM   #17 (permalink)
Twitter: fabiankr
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

My 100th Post a bump...

I encountered a problem and can't find the reason.
I created a new subclass of UIViewController and modified the shouldautorotate method:
Code:
NSLog(@"Rotating");
return YES;
The viewController only get's active as a modalView, but this shouldn't be a problem.

Problem is, that this method never gets called!

In the parentViewController the same method return UIInterfaceOrientationPortrait.
I want this, only this, view to autorotate!

Please help me out...

regards

edit:
It's working now... I don't know why. I didn't modify anything! crazy^^

Last edited by Forsworn; 12-16-2008 at 11:21 AM.
Forsworn is offline   Reply With Quote
Old 12-16-2008, 12:01 PM   #18 (permalink)
Twitter: fabiankr
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

Sorry for this many bumps but it's important and others will need this, too:

It didn't compile without this:
Code:
#define degreesToRadian(x) (M_PI * x / 180.0)
Only rotate a single view:
Code:
- (void)viewWillAppear:(BOOL)animated {	
	_originalTransform = [self.view transform];
	_originalBounds = [self.view bounds];
	_originalCenter = [self.view center];
	
	CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
	landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);
	
	[self.view setTransform:landscapeTransform];
	
	self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	self.view.autoresizesSubviews = YES;
	self.view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
	self.view.center  = CGPointMake (240.0, 160.0);
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}

- (void) viewWillDisappear:(BOOL)animated {
	[self.view setTransform:_originalTransform];
	[self.view setBounds:_originalBounds];
	[self.view setCenter:_originalCenter];
	
	[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}
It's working great but I get a problem with the original navigationbar:

Anyone else encountering the same problem?

regards
Forsworn is offline   Reply With Quote
Old 02-27-2009, 03:25 PM   #19 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 15
Default A (partial) solution

I found A solution. It might not be THE solution, but at least it works. I messed around with the code above but could never get rid of the white bar on the left where the status bar was. This solution takes care of that by creating a whole new window.

Blogged here:

iPhone programming: how to switch to a landscape view at the moment of your choosing

You don't get to keep the continuity of the nav bar but I'm sure you can figure out how to fake it out.
sbwoodside is offline   Reply With Quote
Old 03-03-2009, 02:52 PM   #20 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
Send a message via AIM to kallipigous
Default

Quote:
Originally Posted by sbwoodside View Post
I found A solution. It might not be THE solution, but at least it works. I messed around with the code above but could never get rid of the white bar on the left where the status bar was. This solution takes care of that by creating a whole new window.

Blogged here:

iPhone programming: how to switch to a landscape view at the moment of your choosing

You don't get to keep the continuity of the nav bar but I'm sure you can figure out how to fake it out.
that worked well

My view is shifted 10 or so pixels to the right though. Any idea why?
kallipigous is offline   Reply With Quote
Old 03-03-2009, 03:36 PM   #21 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
Send a message via AIM to kallipigous
Default

Quote:
Originally Posted by kallipigous View Post
that worked well

My view is shifted 10 or so pixels to the right though. Any idea why?
Forget it it was the status bar, I corrected it accordingly
kallipigous is offline   Reply With Quote
Old 03-03-2009, 11:06 PM   #22 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 15
Default

Quote:
Originally Posted by kallipigous View Post
Forget it it was the status bar, I corrected it accordingly
Awesome I'm glad it worked for you!
sbwoodside is offline   Reply With Quote
Old 03-13-2009, 05:43 PM   #23 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 20
Default How to refresh the screen underneath?

Hi,

I tried this approach (second window) and it worked fine. The previous one, based on transformation only, did not work for me (the transition was not so smooth and there was a blank space in the left side - the size of the status bar).

However, after releasing the second window, I need to update the screen I was in (a table view controller). I need this in order to update the text in the cell that triggered the call to the landscape window. I have implemented the viewWillAppear, and called a reload table in there. This works fine in regular navigation use, but in this case, the viewWillAppear method is not called in the first window when the second one disappears.

Any ideas?
mmira is offline   Reply With Quote
Old 03-13-2009, 08:03 PM   #24 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
Send a message via AIM to kallipigous
Default Actually I've discovered a bug with this route

It works beautifully until the iphone goes to sleep with this window up. When it wakes up the previous window is up but I can't click on anything.

Any thoughts about how to wake up the second window?

Toby
kallipigous is offline   Reply With Quote
Old 03-14-2009, 09:00 AM   #25 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
Send a message via AIM to kallipigous
Default

Quote:
Originally Posted by mmira View Post
Hi,

However, after releasing the second window, I need to update the screen I was in (a table view controller). I need this in order to update the text in the cell that triggered the call to the landscape window. I have implemented the viewWillAppear, and called a reload table in there. This works fine in regular navigation use, but in this case, the viewWillAppear method is not called in the first window when the second one disappears.

Any ideas?
Can you use an NSnotification instead?
kallipigous is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,861
Threads: 49,769
Posts: 213,055
Top Poster: BrianSlick (3,138)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0