如何从UITabBarController页面

时间:2015-05-20 09:13:09

标签: ios uinavigationcontroller uitabbarcontroller viewcontroller

首先,我是初学者。请原谅我这个愚蠢的问题。

我使用UITabBarController创建了两个子页面,第一页包含UIButton,而alsl我创建了另一个页面(DetailPage)。我将按钮和DetailPage连接到segue。但是segue的样式不能设置为push。(这会触发错误,push样式仅用于UINavigatorController)。

那么,当我单击按钮时,如何使用PUSH样式导航到DetailPage?

我尝试了一种新方法,使用UINavigatorController管理第一页和DetailPage,然后将此UINavigatorController嵌入UITabItem。当我完成此操作后,我可以使用push样式导航到DetailPage。但是另一个问题是,当导航DetailPage时,TabBar始终保持在页面底部。我想将所有UITabBarController推到左侧。

我想要的效果如下图: enter image description here

如何优化代码?

1 个答案:

答案 0 :(得分:0)

实现这样的委托(UITabBarControllerDelegate)方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSArray *tabViewControllers = tabBarController.viewControllers;
    UIView * fromView = tabBarController.selectedViewController.view;
    UIView * toView = viewController.view;
    if (fromView == toView)
        return false;
    NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController];
    NSUInteger toIndex = [tabViewControllers indexOfObject:viewController];

    [UIView transitionFromView:fromView
                        toView:toView
                      duration:0.3
                       options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = toIndex;
                        }
                    }];
    return true;
}

这是翻转动画改变动画

推送:

    CATransition* transition = [CATransition animation];
    [transition setDuration:0.3];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setFillMode:kCAFillModeBoth];
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:transition forKey:kCATransition]; 

对于Pop:

    CATransition* transition = [CATransition animation];
    [transition setDuration:0.3];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    [transition setFillMode:kCAFillModeBoth];
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:transition forKey:kCATransition];

现在由你决定,祝你好运....