How do I code for the landscape orientation in iPhone? - iphone

I understand that I can change what interface orientations are supported, but if I want the landscape view to be entirely different, or somewhat different than the portrait view, how do I code for it?
Thank you.

Just load another view controller when the orientation changes. To make things simple, I often use a hidden navigation controller and push and pop the views I want for any particular orientation.

Using a dedicated view controller for each orientation is the easiest approach.
If the only difference is presentation, not controller logic, then you could also try coding a single view controller to swap between two views depending on orientation.
E.g. pseudocode
UIView *landscapeView = ...;
UIView *portraitView = ...;
when orientationChanged
{
if landscape then
[portraitView setHidden:YES];
[landscapeView setHidden:NO];
self.view = landscapeView;
else if portrait then
[landscapeView setHidden:NO];
[portraitView setHidden:YES];
self.view = portraitView;
[self.view setNeedsDisplay];
}

Related

Screen rotation from device orientation and a new UIView

What is the safest and most effective way place a new UIView within the bounds of the App's UIWindow on the top of the view stack when a screen rotation from Portrait to Landscape occurs? Also, when returning from Landscape to Portrait, to remove this subview.
Basically the App is created as most are:
-UIWindow:
--UIView
---All subviews (including a tabview controller)
I would like to create:
-UIWindow:
--UIView (new, to be placed on top)
--UIView
---All subviews (and triggered by a view controller in here)
Is this wrong? There is surprisingly little documentation to help do this.
If you create a view controller to hold all of your subviews you can just use the rotation functions that will be called for you:
willRotateToInterfaceOrientation:duration:
and
didRotateFromInterfaceOrientation:
So lets say you use didRotateFromInterfaceOrientation you check
if(UIInterfaceOrientationIsLandscape(orientation))
{
[yourView removeFromSuperView];
}
else
{
[self.view addSubView: yourView];
}
See my answer here: https://stackoverflow.com/a/4960988/202451
It should bring you closer to doing custom things like that
I found the a working solution. Might you offer a better one?
ArtsDayAppDelegate *appDelegate = (ArtsDayAppDelegate*)[[UIApplication sharedApplication] delegate];
UIView *landscapeView;
if (!landscapeView) {
landscapeView = [[UIView alloc] initWithFrame: frame];
}
[landscapeView setBackgroundColor:[UIColor blackColor]];
..add various views..
[appDelegate.window.rootViewController.view addSubview:landscapeView];

Layout Multiview Rotated iPhone App with a Toolbar

I have a multiview app with a Toolbar. The Root view controller controls the toolbar, but the other views have their own view controller class. I use code like this to load the views:
-(IBAction)loadBand1Start:(id)sender{
[self clearView];
Band1Start *band1Controller = [[Band1Start alloc] initWithNibName:#"Band1Start" bundle:nil];
self.band1Start = band1Controller;
[band1Controller release];
[self.view insertSubview:band1Start.view atIndex:0];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
band1Start.view.frame=CGRectMake(0, 0, 480.0, 260.0);
} else {
band1Start.view.frame=CGRectMake(0, 0, 320.0, 400.0);
}
}
The IF statement is to make it load in the correct orientation. Without this, although it rotates ok, it always loads in Portrait, even if the phone is in landscape.
Autosize and Auto Rotate works. I used IB to build my nibs in portrait. When the phone is rotated to landscape, they do rotate and resize, however with some nibs, the rotated layout does not look right. There is overlaps etc, which is a common problem, and I know can be fixed using CGRectMake.
However, it does not matter where I put the CGRectCode, it does not execute. I have tried putting it in willRotateToInterfaceOrientation, willAnimateRotationToInterfaceOrientation, and viewWillAppear, but the rotated layout is not changed.
If I use the code below to switch views, the rotated layout is how I want it, but the toolbar does not appear:
-(IBAction)loadBand1Start:(id)sender{
[self clearView];
Band1Start *band1Controller = [[Band1Start alloc] initWithNibName:#"Band1Start" bundle:nil];
[self presentModalViewController:band1Controller animated:NO];
[band1Controller release];
}
I think this is because the view controller for the toolbar needs to be the root view controller for the toolbar to appear, but only the root view controller receives the orientation change notifications.
So if I keep my toolbar controller as the root view controller, the subview's controller does not receive the orientation notifications, so does not execute my CGRectMake code, but if I make the subview's controller the root view controller, it receives the notifications ok, but the toolbar does not appear because it's controller is not the root controller.
Am I correct about the problem here? Is there a way around it?

Auto-Resizing a dynamic view when rotated

I have a tab bar application where everything is working fine. I have rotations of the device all working fine with the various Tab Bar View controllers.
Alas it was suggested that a couple of the View Controllers needed a help page. To this end I created a new ViewController that contains a UIWebView (where help can be built into an HTML file).
I create the new "HelpViewController" as follows:
mpHelpPage = [[HelpPageViewController alloc] init];
[mTabBarController.view addSubview: mpHelpPage.view];
[mpHelpPage retain];
mpHelpPage.view.alpha = 0.75f;
This brings up the help page no problems when I'm in portait mode. Unfortunately when I'm in landscape mode and I do the above code it adds the HelpViewController in Portrait (meaning it extends off the bottom of the screen).
As such when I alloc the ViewController above I need some way of telling the ViewController to rotate to the current device orientation.
I am, however, at a loss as to how to get it to do this. Any help would be much appreciated!
I handle this annoyance by putting an orientation check in viewWillAppear:, e.g.
if (self.interfaceOrientation == UIInterfaceOrientationPortrait ||
self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
// custom code or call willRotate
} else {
// custom code or call willRotate
}
You can also do this if you prefer
if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) {
// custom code or call willRotate
} else {
// custom code or call willRotate
}
you should either set the frame-property of your subview in willRotateToInterfaceOrientation:duration: of your ViewController
or you write your own View and set the frame-property in layoutSubviews of your View
The added Subview should handle the layout of its subviews.
Since you've added HelpViewController as a subview and no UIViewController controls it, it will not be resized. You can resize HelpViewController's view manually by detecting a change in the orientation in the shouldAutorotateToInterfaceOrientation: method of the current UIViewController. This method passes the current orientation as its argument, so just check which is the current orientation and set a frame accordingly as:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight))
mpHelpPage.view.frame = CGRectMake(0,0,480,300);
else
mpHelpPage.view.frame = CGRectMake(0,0,320,460);
return YES;
}
Or, Instead of adding HelpViewControlleras a subView, try [self.navigationController pushViewController:HelpViewController animated:YES];

UINavigationController and Alternate Landscape

In my application I use an Alternate Landscape Interface strategy (present your landscape view as a modal). I also use a navigation controller for transitioning and this causes the following problem: I dunno how to push/pop correctly from landscape orientation.
I came up with the following solution, but someone may know a better one. Suppose one has to deal with only two views. Let's call them AP, AL, BP, BL, where the second letter stands for orientation. We start with a navigation controller with AP inside. To go between AP and BP we just push/pop. To go from AP to AL we present a modal navigation controller with AL inside. To go between AL and BL we push/pop inside the second navigation controller. Now to go from BP to BL we pop w/o animation and present a modal navigation controller with BL sitting on top of AL. To go from BL to BP we dismiss the modal navigation controller and push BP w/o animation.
Seems to be a bit ugly, but not so bad. Can anyone think of something better?
Thanks in advance!
Is there some reason you need to present your landscape orientation as modal in a separate controller? When I have two entirely different views for my portrait and landscape orientations I fade between them as they stretch during the rotation.
This allows for vastly different content in both orientations, a nice transition between them, and shared code under one controller.
Here is some code. Our UIViewController will switch between portraitView and landscapeView when we change orientation.
portraitView and landscapeView are both children of the UIViewController's view. The hierarchy looks as follows:
UIViewController
|
- view
|
|- portraitView
|
|- landscapeView
Both have their autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight to ensure that they stretch as the view controller rotates.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if( orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight )
{
[UIView animateWithDuration:duration
animations:^
{
//Fade the landscape view over the top of the
//portrait view as during rotation
landscapeView.alpha = 1.0f;
}
completion:^(BOOL finished)
{
//Hide the portrait view when landscape is fully
//visible
portraitView.alpha = 0.0f
}];
}
else
{
//Show the portrait view (underneath the landscape view)
portraitView.alpha = 1.0f;
[UIView animateWithDuration:duration
animations:^
{
//Fade out the landscape view to reveal the portrait view
landscapeView.alpha = 0.0f;
}];
}
}
Your controls and subviews will fade and deactivate along with the appropriate views, allowing you to have completely different content. I used this recently to fade between two different background images when changing orientation. The effect is very smooth.
You can now create your two view controllers, A and B which each manage two views as described above. You can then simply push the view controllers as normal and not have to worry about managing the UINavigationController's view controller stack during rotation.

iPhone screen rotates at random ?

I use a tabBar Controller as root controller. It has 4 tabs and each of its ViewControllers has
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
as well as the tabBarController itself.
But when I rotate the device (real or simulator), the screen turns at random! If it doesn't turn when I open the application it would have the same behavior until I quit the app.
I tried to add the 4 viewControllers one by one in IB to see if one was problematic, but I obtained the same issue. It only always turns when there is no tabs at all!
Please tell me if you have any ideas. Thanks!
You set every view controller to say that it responds to any possible orientation. Therefore, every view will attempt to rotate to every orientation.
Views don't really automatically rotate. You usually have to manage the placement of subview programmatically in all but the simplest views.
If you have no custom orientation code, you're probably seeing the views try to draw the portrait view in the landscape frame or vice versa. If you have autoresize subviews set your subviews will appear to scatter across the screen in a seemingly random pattern. The more you change orientation, the more random the placement becomes.
For complex views, I like to create separate viewController/view pairs for each orientation. Then I put the views in a nav controller. As the orientation changes, each view controller will push or pop the appropriate view controller for the coming orientation onto/off the stack. To the user, this looks like a single view is gracefully redrawing itself. (This is especially useful if you have non-standard UI elements that have to be manually rotated with transforms)
You have to subclass UITabBarController and implement shouldAutorotateToInterfaceOrientation:
Actually, I just want my first tab view controller to rotate. So I put this code in my custom tabBarController :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (self.selectedIndex == 0) {
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}else {
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
}
but I had the same problem. I use a custom orientation code for my first tab view controller when turning to landscape. Called with the following function in my custom tabBarcontroller:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
//rotation to Portrait
lastOrientation = toInterfaceOrientation;
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
//rotation to Landscape
[self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
lastOrientation = toInterfaceOrientation;
}
}
I found that if you set the selected tab programmatically the tabViewController rotates erratically.