ios – 为什么我的superview-with-subview在以模态方式呈现时会缩小(使用自动布局)?

我有一个视图控制器拥有一个自定义视图;自定义视图使用Core Graphics绘制游戏板. (不涉及其他子视图.)

我设置了自动布局约束,以便游戏板填充其超级视图.当我将视图控制器设置为根视图控制器时,游戏板(和superview)填充屏幕,这是我的意图.但是,当我以模态方式呈现视图控制器时,游戏板(和superview)缩小到空/最小,并且自动布局跟踪报告布局不明确.

我写了一个简化的测试用例来说明这个问题.

这是我的BoardViewController,它有一个带有绿色背景的顶层视图,但是创建了一个带有红色背景的单个扩展子视图.当这个视图控制器接管时,我想看到屏幕都是红色的:

- (void)loadView
{
  UIView *mainView = [[UIView alloc] init];
  mainView.translatesAutoresizingMaskIntoConstraints = NO;
  mainView.backgroundColor = [UIColor greenColor];

  UIView *subView = [[UIView alloc] init];
  subView.translatesAutoresizingMaskIntoConstraints = NO;
  subView.backgroundColor = [UIColor redColor];

  [mainView addSubview:subView];
  self.view = mainView;

  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(subView);
  NSArray *c1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView(>=10)]|"
                                                        options:0
                                                        metrics:nil
                                                          views:viewsDictionary];
  NSArray *c2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView(>=10)]|"
                                                        options:0
                                                        metrics:nil
                                                          views:viewsDictionary];
  [self.view addConstraints:c1];
  [self.view addConstraints:c2];
}

如果我将它设置为我的根视图控制器,那么我会看到我漂亮的红色游戏板填满屏幕.但是,当我从另一个RootViewController呈现BoardViewController时,游戏板缩小到最小10×10平方:

- (void)loadView
{
  UIView *rootView = [[UIView alloc] init];
  rootView.translatesAutoresizingMaskIntoConstraints = NO;

  UIButton *presentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  presentButton.translatesAutoresizingMaskIntoConstraints = NO;
  [presentButton setTitle:@"Present" forState:UIControlStatenormal];
  [presentButton addTarget:self
                    action:@selector(present:)
          forControlEvents:UIControlEventTouchUpInside];

  [rootView addSubview:presentButton];
  self.view = rootView;

  [rootView addConstraint:[NSLayoutConstraint constraintWithItem:presentButton
                                                       attribute:NSLayoutAttributeCenterX
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:rootView
                                                       attribute:NSLayoutAttributeCenterX
                                                      multiplier:1.0
                                                        constant:0.0]];
  [rootView addConstraint:[NSLayoutConstraint constraintWithItem:presentButton
                                                       attribute:NSLayoutAttributeCenterY
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:rootView
                                                          attribute:NSLayoutAttributeCenterY
                                                      multiplier:1.0
                                                      constant:0.0]];
}

- (void)present:(id)sender
{
  BoardViewController *bvc = [[BoardViewController alloc] init];
  [self presentViewController:bvc animated:YES completion:NULL];
}

我一直在尝试不同的自动布局规则,但无论我做什么,当视图控制器以模态方式呈现时,我无法让游戏板填满屏幕. (与此同时我问这个问题,我试图让运行时告诉我它的想法是不明确的.但是我的调试还不是很好,因此我的问题在这里.)

解决方法

去掉:

mainView.translatesAutoresizingMaskIntoConstraints = NO;

这很有意思.这意味着当使用presentViewController以模态方式呈现视图时,它应该具有将其与其superview相关联的约束,但是当设置为UIWindow的rootViewController时,它不需要它们……

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...