Swapping subViews in willAnimateRotation


#1

Hi All,

I am having trouble swapping subviews in willAnimateRotationToInterferacOrientation:duration.

For the subView that I am using when the phone is in portrait mode the width keeps on getting larger and the height keeps on getting smaller as the phone is rotated back and forth.

Here is my code: (hz and vt and view controllers for the subViews being swapped in out of HeavyViewController’s view)

[code]
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
	//Portrait mode
	if (activeView != [vt view]) {
		[[hz view] removeFromSuperview];
		[[self view] addSubview:[vt view]];
		
		activeView = [vt view];
		
		//For debugging
		CGRect rect = [activeView frame];
		NSLog(@"In willAnimate..: the frame for the vertical view is %@", [NSValue valueWithCGRect:rect]);
	}
	
} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
	if (activeView != [hz view]) {
		[[vt view] removeFromSuperview];
		[[self view] addSubview:[hz view]];
		
		activeView = [hz view];
	}
	
}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

  • (void)viewDidLoad {
    // [super viewDidLoad];

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    [[self view] setFrame];

    [[self view] addSubview:[vt view]];
    activeView = [vt view];
    CGRect rect = [activeView frame];
    NSLog(@“In viewDidLoad: the frame for the vertical view is %@”, [NSValue valueWithCGRect:rect]);

}[/code]

Here is the debugger output:
2010-07-08 15:23:46.264 HeavyRotation[1610:307] In viewDidLoad: the frame for the vertical view is NSRect: {{0, 0}, {320, 460}}
2010-07-08 15:23:46.301 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:23:46.465 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:23:52.081 HeavyRotation[1610:307] OrientationChanged: 4
2010-07-08 15:23:54.483 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:23:54.576 HeavyRotation[1610:307] In willAnimate…: the frame for the vertical view is NSRect: {{0, 0}, {480, 300}}
2010-07-08 15:23:58.687 HeavyRotation[1610:307] OrientationChanged: 4
2010-07-08 15:24:01.991 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:24:02.071 HeavyRotation[1610:307] In willAnimate…: the frame for the vertical view is NSRect: {{0, 0}, {720, 196}}
2010-07-08 15:24:03.992 HeavyRotation[1610:307] OrientationChanged: 4
2010-07-08 15:24:05.793 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:24:05.866 HeavyRotation[1610:307] In willAnimate…: the frame for the vertical view is NSRect: {{0, 0}, {1080, 128}}
2010-07-08 15:24:07.495 HeavyRotation[1610:307] OrientationChanged: 4
2010-07-08 15:24:08.996 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:24:09.059 HeavyRotation[1610:307] In willAnimate…: the frame for the vertical view is NSRect: {{0, 0}, {1620, 83}}
2010-07-08 15:24:15.302 HeavyRotation[1610:307] OrientationChanged: 4
2010-07-08 15:24:16.703 HeavyRotation[1610:307] OrientationChanged: 1
2010-07-08 15:24:16.778 HeavyRotation[1610:307] In willAnimate…: the frame for the vertical view is NSRect: {{0, 0}, {2430, 54}}

Thanks!
Mazen Abdel-Rahman


#2

Just gave it a glance, might come back to it later, but my recommendation is that you do not add a view controller’s view to another view controller’s view directly. This may have be causing the issue. A view controller should manage a single full screen view and all of its subviews. The responsibility of hz and vt’s view is that of this view controller you are showing the implementation for. Warring view controllers seldom play nicely together.


#3

Hi,

Try this to avoid the conflicting controller problems

Create an instance variable UIView *containerView;

In ViewDidLoad:

	containerView = [[[UIView alloc] initWithFrame] autorelease];     //Create a new UIView 
	[[self view] addSubview:containerView];   //Add it to HeavyViewControllers view
	[containerView addSubview:[vt view]];      //Add your first view to the container view
	activeView = [vt view];
	CGRect rect = [activeView frame];
	NSLog(@"In viewDidLoad: the frame for the vertical view is %@", [NSValue valueWithCGRect:rect]);

and then use the containerView instead of [self view]

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//	CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
	
	
	if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
		//Portrait mode
		if (activeView != [vt view]) {

			[[hz view] removeFromSuperview];						
			[containerView addSubview:[vt view]];  // use container view
			activeView = [vt view];
			
			//For debugging
			CGRect rect = [activeView frame];
			NSLog(@"In willAnimate..: the frame for the vertical view is %@", [NSValue valueWithCGRect:rect]);
		}
		
	} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
		if (activeView != [hz view]) {
			[[vt view] removeFromSuperview];
			[containerView addSubview:[hz view]];  // use container view
			
			activeView = [hz view];
		}
		
	}
	
}

HTH

Gareth


#4

Hi,

I am struggling with the very same thing, do you mind posting more of your source code so I can learn more? I have created a UIView called containerView but the complier keeps saying it will not respond to “view” as it says in those code snippets.

Mike


#5

Hi,

Sorry I don’t have any more source code I just added to the previous post.

Do you have hz & vt defined as UIViewController ?

Gareth


#6

I worked it out eventually - I wasn’t defining them as ViewControllers instead I was making them UIViews which was wrong. What I did in the end was this:

[code]if (x == UIInterfaceOrientationPortrait)
{
NSLog(@“PerformAnalysisViewController: Gone to Potrait”);

           // remove the landscape 
	[tableDataViewController.view removeFromSuperview];
	
            // insert new subView
	hyGraphView *hv = [[hyGraphView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[hv setBackgroundColor:[UIColor whiteColor]];
	[self.view insertSubview: hv atIndex:0];
	[hv release];
	
} 

if ((x == UIInterfaceOrientationLandscapeLeft) || 
	(x == UIInterfaceOrientationLandscapeRight))
{
	NSLog(@"PerformAnalysisViewController: Gone to Landscape");
	
	TableDataViewController *tableController = [[TableDataViewController alloc] initWithNibName:@"TableDataViewController" bundle:nil];
	self.tableDataViewController = tableController;
	[self.view insertSubview: tableController.view atIndex:0];
	[tableController release];
}[/code]

Seems to work, or will I run into the same problem as the original poster?

Mike