【问题标题】:Best way to add a UIViewController on top of another UIViewController在另一个 UIViewController 之上添加 UIViewController 的最佳方法
【发布时间】:2013-03-24 15:18:11
【问题描述】:

在我的 iPhone 上已经存在的 UIViewController 之上添加 UIViewController 的最佳方法是什么。我知道有两种方法。但是有更好的方法吗?如果不是,哪一个更好?

1. [self presentModalViewController:controller animated:NO];
2. [self.view addSubview:someController.view];

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    这取决于您希望如何实现它。如果您想显示视图控制器并使用现有的转换将其关闭,您可以使用 presentModalViewController。但是,如果你想用一些自定义动画来显示它,你可以使用 addSubView。同样,这完全取决于您。

    【讨论】:

    • 所以如果我有一个 UIViewController 并且我想添加一个子视图,我该如何为它设置动画呢?例如,让 UIButton 从右侧飞入?
    • @StanLe,你可以使用 UIView 动画。请参阅此链接,objcolumnist.com/2009/07/18/…
    【解决方案2】:
    [[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) {
    
                     }
    ];
    

    【讨论】:

      【解决方案3】:

      这取决于您显示视图控制器的要求。 导航堆栈中可能还有一个推送控制器。

      [self.navigationController pushViewController:anotherViewController animated:YES];
      

      查看 appl 论坛帖子,了解何时使用 pushViewController: 以及何时使用 presentModalViewController:

      pushViewController versus presentModalViewController

      presentModalViewController vs. pushViewController

      【讨论】:

        【解决方案4】:

        两种方式,几乎是等价的,当做一堆视图时,你可以看到下面的视图(直到不是碱性的),[self removeFromSuperview],当你addSubView,和[self dismissModalViewControllerAnimated:YES];,当你用[self presentModalViewController:tempView animated:NO];,但是是的presentModalViewController,给你动画的默认选项,不管addSubview,你需要为此努力。

        【讨论】:

          【解决方案5】:

          取决于你想要什么。没有一种方法比另一种更好。一切都只是……方式。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-09
            • 2023-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多