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

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

概述我有一个视图控制器拥有一个自定义视图;自定义视图使用Core Graphics绘制游戏板. (不涉及其他子视图.) 我设置了自动布局约束,以便游戏板填充其超级视图.当我将视图控制器设置为根视图控制器时,游戏板(和superview)填充屏幕,这是我的意图.但是,当我以模态方式呈现视图控制器时,游戏板(和superview)缩小到空/最小,并且自动布局跟踪报告布局不明确. 我写了一个简化的测试用例来 我有一个视图控制器拥有一个自定义视图;自定义视图使用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 – 为什么我的superview-with-subview在以模态方式呈现时会缩小(使用自动布局)?全部内容,希望文章能够帮你解决ios – 为什么我的superview-with-subview在以模态方式呈现时会缩小(使用自动布局)?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099762.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存