 |
|
 |
|
 |
08-31-2008, 12:37 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 16
|
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
|
|
|
08-31-2008, 07:55 AM
|
#2 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
|
Use the search function. There's already several topics up here which deal with landscape mode...
|
|
|
09-03-2008, 12:57 PM
|
#3 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Quote:
Originally Posted by Jume
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.
|
|
|
09-03-2008, 01:30 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
|
|
|
|
09-04-2008, 08:19 AM
|
#5 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Quote:
Originally Posted by Jume
|
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.
|
|
|
09-04-2008, 12:37 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
|
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
|
|
|
09-04-2008, 12:51 PM
|
#7 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Quote:
Originally Posted by Jume
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.
|
|
|
09-04-2008, 02:57 PM
|
#8 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
|
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.
|
|
|
09-05-2008, 07:34 AM
|
#9 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Thanks for the advice. I will give it a try and let you know how it goes.
|
|
|
09-09-2008, 08:09 AM
|
#10 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Quote:
Originally Posted by Jume
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.
|
|
|
09-30-2008, 11:14 AM
|
#11 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
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.
|
|
|
10-03-2008, 02:54 AM
|
#12 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 258
|
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
|
|
|
10-03-2008, 08:37 AM
|
#13 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
Can you explain this comment:
Quote:
|
// no scrolling in this case, we don't want to interfere with touch events on edit fields
|
|
|
|
10-09-2008, 03:23 PM
|
#14 (permalink)
|
|
New Member
Join Date: Jun 2008
Posts: 22
|
Quote:
Originally Posted by PhoneyDeveloper
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.
|
|
|
12-15-2008, 09:33 AM
|
#15 (permalink)
|
|
New Member
Join Date: Dec 2008
Posts: 1
|
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
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;
}
|
|
|
|
12-15-2008, 04:19 PM
|
#16 (permalink)
|
|
Twitter: fabiankr
Join Date: Oct 2008
Location: Germany
Posts: 504
|
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.
|
|
|
12-16-2008, 07:01 AM
|
#17 (permalink)
|
|
Twitter: fabiankr
Join Date: Oct 2008
Location: Germany
Posts: 504
|
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.
|
|
|
12-16-2008, 12:01 PM
|
#18 (permalink)
|
|
Twitter: fabiankr
Join Date: Oct 2008
Location: Germany
Posts: 504
|
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
|
|
|
02-27-2009, 03:25 PM
|
#19 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 15
|
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.
|
|
|
03-03-2009, 02:52 PM
|
#20 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
|
Quote:
Originally Posted by sbwoodside
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?
|
|
|
03-03-2009, 03:36 PM
|
#21 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
|
Quote:
Originally Posted by kallipigous
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
|
|
|
03-03-2009, 11:06 PM
|
#22 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 15
|
Quote:
Originally Posted by kallipigous
Forget it it was the status bar, I corrected it accordingly
|
Awesome I'm glad it worked for you!
|
|
|
03-13-2009, 05:43 PM
|
#23 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 20
|
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?
|
|
|
03-13-2009, 08:03 PM
|
#24 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
|
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
|
|
|
03-14-2009, 09:00 AM
|
#25 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Location: Los Angeles
Posts: 38
|
Quote:
Originally Posted by mmira
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?
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 421 |
| 34 members and 387 guests |
| AdamSubach, aderrington, benoitr007, bensj, BrianSlick, Danneman, dev123, ErichGS, gustavo7sexton, gw1921, HemiMG, iSDK, Jeremy1026, lifeCoder45, melodizzzy, mriphoneman, newchucky, Ovidius, Paul10, Piequanna, pofak, qilin, Racker, raheel, rendezvouscp, riq, Sega dude, socals, timle8n1, Whitehk, ZunePod |
| Most users ever online was 965, 06-30-2010 at 04:26 AM. |
» Stats |
Members: 41,861
Threads: 49,769
Posts: 213,055
Top Poster: BrianSlick (3,138)
|
| Welcome to our newest member, melodizzzy |
|