I have a UINavigationController that has a visible Navigation Bar and Toolbar. When pushing a controller (the same controller), I want to only animate the content portion (with a sliding effect for example), and keep the NavigationBar and Toolbar unanimated/constant. Can anyone offer any suggestions on a clean way of doing this? Thanks.
+1
A:
I'd move the views manually and animate them using a animation block:
// Get position of old view controller
CGPoint center = [oldViewController.view center];
// Position new view controller to the right of old one
[newViewController.view setCenter:CGPointMake(center.x + 320, center.y)];
// Animate change
[UIView beginAnimations:@"myAnimation" context:NULL];
// Move old view off screen
[oldViewController.view setCenter:CGPointMake(center.x - 320, center.y)];
// Move new view onto screen
[newViewController.view setCenter:center];
// Set animation delegate to self so that we can detect when animation is complete
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationComplete:finished:target:)];
// Finish animation block
[UIView commitAnimations];
If you need to actually push the new viewcontroller using the navigation controller you need to make sure you do this after the animation has finished otherwise the old view will not be displayed. To do so you need to use the setAnimationDelegate method to get notified of when the animation is complete and pass in a selector method to be called. Implement the above code and the following method in the same class:
(void)animationComplete:(NSString *)animationId
finished:(BOOL)finished
target:(UIView *)target
{
if([animationId isEqualToString:@"myAnimation"])
{
// Push viewcontroller here
}
}
pheelicks
2010-02-24 11:26:02
Thx. It almost works except for the oldViewController view disappearing after it's pushed. When the animation takes place, the old view is just a black color. Do you know how to keep the oldViewController.view visible after it's pushed? This is the code I added to set oldViewController and newViewController. CGPoint center = [self.view center]; UIViewController* oldViewController = [self.navigationController topViewController];NewController* newViewController = [[[NewController alloc] init] autorelease];[self.navigationController pushViewController:newViewController animated:NO];
JStay
2010-02-25 02:11:38
You need to end the animation block before you push in the new view contoller. That is, call [UIView commitAnimations] before [self.navigationController pushViewController:newViewController animated:NO]
pheelicks
2010-02-25 09:09:53
Thanks for your response. I moved the push to after the commit, and it still produces the same result with the new view transitioning into a black/empty view. I tried the code without the push to see if the oldView transitions out, and it works, but once I introduce the push, it disappears.
JStay
2010-02-25 09:54:28
I see what's happenening, you need to push the new view controller after the animation has completed. See updated answer
pheelicks
2010-02-25 10:57:11
Thanks again! Unfortunately that doesn't seem to work either :( This creates the opposite effect, I see the old controller swipe off, and there is black where the new controller should be, and then the new controller view displays once it's pushed. The problem is that the newViewController hasn't been added to the view hierarchy before I start the animations, it's just initialized. It only appears once it's pushed (at that point the animation is already finished). What I was originally trying to do is to create a transparency b/w the pushed controller and the one below.That didn't work...
JStay
2010-02-25 11:17:01
+1
A:
Try adding the toolbar as a subview to window instead of View. and restrict the navigationcontroller view height so that it toolbar will be visible to you. Now if you push the view controller, Navigation bar and Toolbar will remain unanimated/stagnent.
Manjunath
2010-02-26 06:43:12
I thought of doing this, but it seemed like a big hack. I'll give it a try, and post back. Thanks!
JStay
2010-02-26 07:15:48
I tried this and it works for the most part. The first attempt I adjusted the nav controller height,but then when I would push a modal controller, and dismiss it the height is always returned incorrectly when dismissed (could only changed the height back in viewDidAppear which would give me choppy animation transitioning back).Next, I didn't play with the nav height, just overlaid the toolbar added to the window,but then I had overlap with the modal controller swipping UP b/c the toolbar in the window was on a higher layer then the nav cont. Don't know hot to fix that animation yet. ideas?
JStay
2010-03-05 15:48:02