在另一个UIViewController上方添加一个UIViewController的最佳方法

4

如何在iPhone上已经存在的UIViewController之上添加一个新的UIViewController是最佳方法?我知道两种方法。但是是否有更好的方法?如果没有,那么这两种方法中哪一种更好呢?

1. [self presentModalViewController:controller animated:NO];
2. [self.view addSubview:someController.view];
5个回答

2

这取决于你想要如何实现。如果你想使用现有的转换来显示和解除视图控制器,可以使用 presentModalViewController。但是,如果你想用一些自定义动画来显示它,可以使用 addSubView。再次强调,完全取决于你。


如果我有一个UIViewController并且想要添加一个子视图,我该如何使其动画化呢?例如,让一个UIButton从右边飞入? - StanLe
@StanLe,你可以使用UIView动画。请参考此链接:http://objcolumnist.com/2009/07/18/simple-uiview-based-animations-on-the-iphone/ - EmptyStack

1

1
[[self view] addSubview: [otherViewController view]];

CGRect frame = [[self view] frame];

int direction = 1;
switch (direction) {
    case 0: // from bottom
        frame.origin.y = [[self view] frame].size.height;
        [[otherViewController view] setFrame: frame];
        frame.origin.y = 0.0 - [[self view] frame].size.height;
        break;
    case 1: // from right
        frame.origin.x = [[self view] frame].size.width;
        [[otherViewController view] setFrame: frame];
        frame.origin.x = 0.0 - [[self view] frame].size.width;
        break;
}

[UIView animateWithDuration: 1.0
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     [[self view] setFrame: frame];
                 }
                 completion: ^(BOOL finished) {

                 }
];

0

这两种方式几乎等效,因为它们都使用视图堆栈。你可以通过 [self removeFromSuperview] 来看到下面的视图(直到未被碱化),当你使用 addSubView 时;而当你使用 [self presentModalViewController:tempView animated:NO]; 时,可以通过 [self dismissModalViewControllerAnimated:YES]; 进行操作。但是,presentModalViewController 提供了默认的动画选项,而使用 addSubview 需要进行一些调整。


-1

这取决于你想要什么。没有一种方法比另一种更好。所有的方法都只是...方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接