开发者

How to push sub-ViewControllers without NavigationController?

I want to push viewControllers to an area on my main viewController. From what I understand, you can't use a NavigationController unless you are adding it to a UIWindow.

This animation reminds of UIScrollView horizontal scrolling. But I want it to work like a NavigationController instead. The ViewControllers will be changed when the user presses on specific actions.

ViewController2 is going to be a tableView with some other buttons.

开发者_StackOverflow中文版

ViewController3 is going to be a form to change the object from the tableView.

How to push sub-ViewControllers without NavigationController?


I recently found a method in UIView that creates a "push or pop" feel to switching views.

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

This is straight out of the UIView documentation, so I would recommend looking at the UIView Class Reference to see the specifics. Basically, it switches a view to another view, while the options: section lets you pick from a multitude of different styles.


I Actually solved it by playing around with UIView Animations.

-(void)slideInView:(UIView*)newView toView:(UIView*)oldView {
    /* Sets the view outside the right edge and adds it to mainView */
    newView.bounds = mainView.bounds;
    newView.frame = CGRectMake(newView.frame.origin.x + 784, newView.frame.origin.y, newView.frame.size.width, newView.frame.size.height);
    [mainView addSubview:newView];

    /* Begin transition */
    [UIView beginAnimations:@"Slide Left" context:oldView];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDelay:0.0f];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDidStopSelector:@selector(slideInCompleted:finished:context:)];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    newView.frame = CGRectMake(0,0, newView.frame.size.width, newView.frame.size.height);
    oldView.frame = CGRectMake(oldView.frame.origin.x - 784, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);
    [UIView commitAnimations];
}

-(void)slideInCompleted:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    UIView *view = (UIView*)context;
    [view removeFromSuperview];
}

and to use it

menuController = [[MenuViewController alloc] init];
[self slideOutView:menuController.view toView:loginController.view];
[oldController release];

Remeber that you have alloced a UIViewController that you need to release when you are done useing it! In example i'm done with loginController and releasing it.


There's absolutely no way to "push" natively any controller without a UINavigationController. You can however, animate views in and out.

With the example you have posted, it looks like a simple UIScrollView would be your easiest option.


I’d use a UIScrollView, as it has the ability to do snapping to "pages" built in. You can also scroll to any part with setContentOffset:animated: as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜