iPhone Pushing View Controller in a left direction

17,192

Solution 1

Instead of using a navigation controller, I would just move the view.

CGRect inFrame = [currentView frame];
CGRect outFrame = firstFrame;
outFrame.origin.x -= inFrame.size.width;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];

[newView setFrame:inFrame];
currentView setFrame:outFrame];

[UIView commitAnimations];

Solution 2

I have done change animation direction when we push viewcontroller. you can change animation type here [animation setSubtype:kCATransitionFromRight];

 ViewController *elementController = [[ViewController alloc] init];

// set the element for the controller
ViewController.element = element;


// push the element view controller onto the navigation stack to display it

CATransition *animation = [CATransition animation];
[[self navigationController] pushViewController:elementController animated:NO];
[animation setDuration:0.45];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[[elementController.view layer] addAnimation:animation forKey:@"SwitchToView1"];

[elementController release];

Solution 3

I don't think you can explicitly define sliding direction in UINavigationControllers. What you might be able to do is pop the current view off the navigation stack to show the prior view, which would animate in the manner you want. However this may be complex if you want to have different view controllers appear depending on what you do on the current view.

If your workflow is not too complicated, you can hold a reference to the prior view controller in the current view controller. depending on what you do on the current view (like select a table view cell), you can change whatever data you need in the prior view controller, and then call

[self.navigationController popViewController];

or whatever the correct method is (i think that's pretty close to how it is). that would let you move down the nav stack with the animation you want, which works if your nav stack has a set number of views on it.

Solution 4

to what Reed Olsen said: you must only hook up one button, that starts the slide up to the same method and add a BOOL that tracks if the view is shown or not. all you need to do is set the origin properly.

- (IBAction)slideMenuView 
{
  CGRect inFrame = [self.view frame];
  CGRect outFrame = self.view.frame;

  if (self.viewisHidden) {
    outFrame.origin.x += inFrame.size.width-50;
    self.viewisHidden = NO;
  } else {
    outFrame.origin.x -= inFrame.size.width-50;
    self.viewisHidden = YES;
  }
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.5];
  [self.menuView setFrame:inFrame];
  [self.view setFrame:outFrame];
  [UIView commitAnimations];
}
Share:
17,192
Aran Mulholland
Author by

Aran Mulholland

Full stack, Javascript, HTML, .NET, .NET Core, ASP.NET MVC, Project Coding Infrastructure, iOS, neo4j development, analogue synthesis, toilet photographer.

Updated on July 28, 2022

Comments

  • Aran Mulholland
    Aran Mulholland over 1 year

    I have an app that has a centre view with two views off to each side of it. I want to have two navigation bar buttons, left and right which push a new navigation controller onto the view from the left or the right.

    When you change views by pushing a new view using the pushviewController: method of NavigationController, the view appears to slide in from the right. how do i change this to slide in from the left?

  • Aran Mulholland
    Aran Mulholland almost 15 years
    so load the view controller in the normal manner, then get a handle on the view, then move it? i would also have to manually change the Navigation bar buttons as well
  • Reed Olsen
    Reed Olsen almost 15 years
    Right. Load the view controller as normal, and move currentView off of the screen and move newView in.
  • Aran Mulholland
    Aran Mulholland almost 15 years
    this works (you have to replace the -= with a +=) to slide in from the left. however that means manually doing all the buttons back and forward for each view. what a pain.
  • Aran Mulholland
    Aran Mulholland almost 15 years
    any idea on how to style the buttons so they are the "pointy" type used in navigation usually? (the back button)
  • aiham
    aiham about 13 years
    I haven't found any way of getting the navigation controller's pointy buttons other than using the navigation controller or your own images.
  • Chanchal Raj
    Chanchal Raj over 8 years
    It makes previous view controller black, that is bad thing.