美文网首页iOS开发
关于模态到控制器present

关于模态到控制器present

作者: 追逐_chase | 来源:发表于2017-07-12 13:07 被阅读13次

模态是push之后有一种 跳转到另外一个控制器的方式

1.模态 到 另一个控制器的的方法
[self presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]]
2.退出模态的方法
[self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>].
3.模态的原理

  • 模态时,会把窗口上面的View给移除,把要 模态(modal)进来的控制器的View添加到窗口(window)上
  • 下面模仿一下
  • (1)假设有2个控制器 OneVC, TwoVC,窗口的Window的rootViewContoller是OneVC,
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[OneVC alloc] init];
    [self.window makeKeyAndVisible];
    return YES
    }
  • (2)OneVC实现
    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(handle) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    }
    按钮方法实现--模态
    TwoVC *twoVC = [[TwoVC alloc] init];
    CGRect frame = twoVC.view.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height;
    twoVC.view.frame = frame;
    [[UIApplication sharedApplication].keyWindow addSubview:twoVC.view];
    self.twoVC = twoVC;
    [UIView animateWithDuration:0.5 animations:^{
    twoVC.view.frame = self.view.frame;
    }completion:^(BOOL finished) {
    [self.view removeFromSuperview];
    }];

相关文章

网友评论

    本文标题:关于模态到控制器present

    本文链接:https://www.haomeiwen.com/subject/ljgvhxtx.html