Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Force landscape mode in one view

OpsiveOpsive Posts: 16Registered Users
edited April 2010 in iPhone SDK Development
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):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
		[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
	}
	return self;
}

Thanks,
Justin
Post edited by Opsive on
«1

Replies

  • JumeJume Posts: 264Registered Users
    edited August 2008
    Use the search function. There's already several topics up here which deal with landscape mode...
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    Jume wrote: »
    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.
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    Jume wrote: »

    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:
    - (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
  • JumeJume Posts: 264Registered Users
    edited September 2008
    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
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    Jume wrote: »
    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.
    - (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.
  • JumeJume Posts: 264Registered Users
    edited September 2008
    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 :)
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    Thanks for the advice. I will give it a try and let you know how it goes.
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    Jume wrote: »
    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.
  • cope784cope784 Posts: 22Registered Users
    edited September 2008
    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):
    @interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    {
    	UITableView	*myTableView;
    	CGAffineTransform _originalTransform;
    	CGRect _originalBounds;
    	CGPoint _originalCenter;
    }
    
    @property (nonatomic, retain) UITableView *myTableView;
    
    @end
    

    Next here is the implementation:
    @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.
  • JumeJume Posts: 264Registered Users
    edited October 2008
    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
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited October 2008
    Can you explain this comment:
    // no scrolling in this case, we don't want to interfere with touch events on edit fields
  • cope784cope784 Posts: 22Registered Users
    edited October 2008
    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.
  • DrChuckDrChuck Posts: 1New Users
    edited December 2008
    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
    cope784 wrote: »
    - (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; 
    }
    
  • ForswornForsworn Posts: 504New Users
    edited December 2008
    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. ;)
  • ForswornForsworn Posts: 504New Users
    edited December 2008
    My 100th Post a bump... :eek:

    I encountered a problem and can't find the reason.
    I created a new subclass of UIViewController and modified the shouldautorotate method:
    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^^
  • ForswornForsworn Posts: 504New Users
    edited December 2008
    Sorry for this many bumps but it's important and others will need this, too:

    It didn't compile without this:
    #define degreesToRadian(x) (M_PI * x / 180.0)
    

    Only rotate a single view:
    - (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:
    Bild%201.png
    Anyone else encountering the same problem?

    regards
  • sbwoodsidesbwoodside Posts: 16Registered Users *
    edited February 2009
    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.
  • kallipigouskallipigous Posts: 40Registered Users
    edited March 2009
    sbwoodside wrote: »
    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?
  • kallipigouskallipigous Posts: 40Registered Users
    edited March 2009
    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
  • sbwoodsidesbwoodside Posts: 16Registered Users *
    edited March 2009
    Forget it it was the status bar, I corrected it accordingly

    Awesome I'm glad it worked for you!
  • mmirammira Posts: 20Registered Users
    edited March 2009
    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?
  • kallipigouskallipigous Posts: 40Registered Users
    edited March 2009
    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
  • kallipigouskallipigous Posts: 40Registered Users
    edited March 2009
    mmira wrote: »
    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?
  • jbullfrogjbullfrog Posts: 141Registered Users
    edited April 2010
    What if I am setting this
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    //====================================================
    		//ROTATES VIEW TO LANDSCAPE MODE AND SETS THE STAGE SIZE
    		CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57079633);
    		[window setTransform:rotate];
    		
    		CGRect contentRect = CGRectMake(0, 0, 480, 320); 
    		window.bounds = contentRect; 
    		[window setCenter:CGPointMake(160.0f, 240.0f)];
    		//====================================================
    
    }
    

    This forces the app to be in landscape right (home button right)

    But what if I need to keep this, and also support landscape left?

    Could anyone help me figure this out?

    Thanks
«1
Sign In or Register to comment.