PDA

View Full Version : View Controller and device orientation question




BrooklynDon
Jan 20, 2011, 02:27 PM
Hello again folks!
Thanks for answering my first question. Now, here's my next question. My first screen works great in both landscape and portrait mode, but when i click a button to show another view, I get the landscape view with the portrait mode of the main screen. Here's how it looks on the iPhone simulator:

Here's the code for the main screen:

//Import all of the view controllers that we will use

#import "DownstateViewController.h"
#import "aboutDownstateViewController.h"
#import "AboutThisAppViewController.h"
#import "downstateServicesViewController.h"
#import "searchDownstateViewController.h"

@implementation DownstateViewController

@synthesize downstate;
@synthesize welcomeToDownstate;
@synthesize aboutDownstate;
@synthesize downstateServices;
@synthesize searchDownstate;
@synthesize aboutThisApp;
@synthesize redAlert;
@synthesize alertMessage;
@synthesize copyright;

aboutDownstateViewController *viewController;

//create the orientation views (Portrait and Landscape)
-(void)positionViews {
UIInterfaceOrientation destOrientation = self.interfaceOrientation; //Get the presengt orientation from the device
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//Show Portrait Mode
downstate.frame = CGRectMake(0, 0, 321, 175);
welcomeToDownstate.frame = CGRectMake(30, 190, 261, 45);
aboutDownstate.frame = CGRectMake(20, 266, 117, 50);
downstateServices.frame = CGRectMake(20, 324, 117, 50);
searchDownstate.frame = CGRectMake(183, 266, 117, 50);
aboutThisApp.frame = CGRectMake(183, 324, 117, 50);
redAlert.frame = CGRectMake(0, 406,320, 54);
alertMessage.frame = CGRectMake(20, 0, 280, 54);
copyright.frame = CGRectMake(26, 377, 268, 21);
} else {
//Show Landscape Mode
downstate.frame = CGRectMake(149, 0, 186, 101);
welcomeToDownstate.frame = CGRectMake(110, 102, 261, 45);
aboutDownstate.frame = CGRectMake(101, 145, 117, 50);
downstateServices.frame = CGRectMake(101, 203, 117, 50);
searchDownstate.frame = CGRectMake(264, 145, 117, 50);
aboutThisApp.frame = CGRectMake(264, 203, 117, 50);
redAlert.frame = CGRectMake(0, 267, 480, 33);
alertMessage.frame = CGRectMake(20, -11, 440, 54);
copyright.frame = CGRectMake(107, 249, 268, 21);
}
}

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO rientation duration:(NSTimeInterval)duration {
[self positionViews];//set and animate the new position view
}

//Set up the button clicked actions
-(IBAction) button1Clicked: (id) sender{//Button 1 (About SUNY Downstate) is clicked
viewController = [[aboutDownstateViewController alloc] initWithNibName:@"AboutDownstateViewController"
bundle:nil];//load the destination page into the viewController
[UIView beginAnimations:@"flipping view" context:nil];//set the effect to show
[UIView setAnimationDuration:1];//set the duration of the animation. In this case 1 second
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//set the "curve" of the animation
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view
cache:YES];//set the animation between views
[self.view addSubview:viewController.view];
[UIView commitAnimations];//do the animation
}




And here is the code for the subview



#import "AboutThisAppViewController.h"

@implementation AboutThisAppViewController

@synthesize header;
@synthesize text;
@synthesize mainButton;

-(IBAction) btn4Clicked:(id) sender{
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view.superview
cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}

-(void)positionViews{
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait) {
header.frame = CGRectMake(0, 0, 280, 21);
} else {
header.frame = CGRectMake(100, 100, 200, 21);
}
}

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO rientation duration:(NSTimeInterval)duration{
[self positionViews];
}



What do I have to add/subtract from my code to get my subview to match the orientation of my device? TIA



carlosbutler
Jan 20, 2011, 04:17 PM
you need to resize all the views that are linked to it i believe. so if there is a hierarchy, all views need to be resized.

Also, I am not sure if you have it in your code. But when the view loads, you can check to see how the device is being held, and reframe the view(s) accordingly.

BrooklynDon
Jan 21, 2011, 08:42 AM
Hello again folks!
I think I found my answer. I have to un-comment the shouldAutorotateToInterfaceOrientation subroutine. Thanks for the answers, but in the words of that famous philosopher Homer, "D'oh!" :o