【问题标题】:Instantiate View behind current ViewController在当前 ViewController 后面实例化 View
【发布时间】:2023-03-24 13:30:01
【问题描述】:

我想实例化一个 viewController 并将它放在当前显示的视图后面。然后移动原始视图的框架以显示其后面的视图。

我不能先创建底部视图,然后在顶部添加顶部视图。我将创建多个底部视图,内存无法一次处理整个堆栈。

我已经遇到的问题。

  • 添加子视图并将其发送到后面意味着移动原始视图的框架移动整个视图,而不是显示新视图。
  • 实例化新视图并调用 presentViewController 释放原始视图(如果我以模态方式添加)

有人可以帮忙吗?或者给我一个方向?

【问题讨论】:

  • 以模态方式呈现第二个视图控制器不会释放执行呈现的视图控制器。使用presentingViewController会给你一个指向那个VC的指针

标签: iphone uiview uiviewcontroller subview


【解决方案1】:

公平地说,您要从 vc1 过渡到 vc2,您想要的是 外观,即 vc2 在 vc1 下方,而 vc1 滑开以显示它?

如果是这样,那么从 sdk 的角度来看,这在不做任何不寻常或危险的事情的情况下是可行的。诀窍是执行正常的实例化和呈现步骤,但是在 vc1 中,在呈现 vc2 之前,将一个看起来像 vc1 的 UIImage 交给它。 Vc2 在该图像出现之前将其自身覆盖,然后将图像滑开以显示其自身。

步骤如下:

1) 在vc1上,实现方法in this post。它捕获视图的图像。

2) 有一些动作让你想展示vc2,就这样吧……

- (void)presentVc2:(id)sender {
    UIImage *image = [self makeImage];  // it was called makeImage in the other post, consider a better name
    MyViewController2 *vc2 = [[MyViewController2 alloc] initWithNibName:@"MyViewController2" bundle:nil];
    vc2.presentationImage = image;  // more on this later

    // this line will vary depending on if you're using a container vc, but the key is
    // to present vc2 with NO animation
    [self presentViewController:vc2 animated:NO completion:^{}];
}

3) 在 MyViewController2 上创建一个名为 presentationImage 的 UIImage 属性,并将其设置为 public。然后在 MyViewController2 ...

// before we appear, cover with the last vc's image
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:self.presentationImage];
    imageView.frame = self.view.bounds;
    imageView.tag = 128;
    [self.view addSubview:imageView];
}

// after we appear, animate the removal of that image
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:128];
    [UIView animateWithDuration:0.5 animations:^{
        imageView.frame = CGRectOffset(imageView.frame, -self.frame.size.width, 0); 
    }];
}

【讨论】:

    【解决方案2】:

    您可以简单地将顶视图的内容放入一个新的 UIView 中,该 UIView 的框架与您的视图控制器的视图的框架相同。然后将底部视图粘贴在容器视图下方。然后,移动容器视图将移动其所有内容,但保留底部视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      相关资源
      最近更新 更多