【发布时间】:2013-05-28 13:54:40
【问题描述】:
我有一个视图控制器,它拥有一个自定义视图;自定义视图使用 Core Graphics 绘制游戏板。 (不涉及其他子视图。)
我设置了自动布局约束,以便游戏板填充其超级视图。当我将视图控制器设置为根视图控制器时,游戏板(和超级视图)会填满屏幕,这就是我的意图。但是,当我以模态方式呈现视图控制器时,游戏板(和超级视图)缩小到零/最小,并且自动布局跟踪报告布局不明确。
我写了一个简化的测试用例来说明这个问题。
这是我的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 时,游戏板会缩小到最小 10x10 方格:
- (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];
}
我一直在尝试不同的自动布局规则,但无论我做什么,当视图控制器以模态方式呈现时,我都无法让游戏板填满屏幕。 (同时我在问这个问题,我试图让运行时告诉我它认为什么是模棱两可的。但我还不太擅长这个调试,因此我的问题在这里。)
【问题讨论】:
标签: ios autolayout