HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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];
}

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

解决方法

去掉: @H_811_2@mainView.translatesAutoresizingMaskIntoConsTraints = NO;

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

大佬总结

以上是大佬教程为你收集整理的ios – 为什么我的superview-with-subview在以模态方式呈现时会缩小(使用自动布局)?全部内容,希望文章能够帮你解决ios – 为什么我的superview-with-subview在以模态方式呈现时会缩小(使用自动布局)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。