【问题标题】:AutoLayout - Visual Format Language not working iOS8自动布局 - 视觉格式语言不起作用 iOS8
【发布时间】:2014-11-25 07:40:35
【问题描述】:

我正在尝试学习如何使用自动布局和使用视觉格式语言编写约束。对于一个简单的测试,我有一个测试视图并希望它扩大全尺寸。所以顶部,底部,尾随,导致超级视图都为零。

如果我在 iOS 7 中运行,它似乎大部分都可以工作,但在 iPhone 5 模拟器的顶部和底部出现黑条,并且没有模糊的布局错误。

在 iOS 8 中,我遇到了模棱两可的布局错误。谁能告诉我我的问题出在哪里?

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    testView = [UIView new];
    testView.backgroundColor = [UIColor blueColor];
    testView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:testView];

    [self.view needsUpdateConstraints];
}
- (void)updateViewConstraints
{
    [super updateViewConstraints];
    [self.view removeConstraints:self.view.constraints];

    NSDictionary *viewsDictionary =
    NSDictionaryOfVariableBindings(testView);
    NSArray *constraints = [NSLayoutConstraint
                            constraintsWithVisualFormat:@"|[testView]|"
                            options:0
                            metrics:nil
                            views:viewsDictionary];
    [self.view addConstraints:constraints];

    constraints = [NSLayoutConstraint
                   constraintsWithVisualFormat:@"V:|[testView]|"
                   options:0
                   metrics:nil
                   views:viewsDictionary];
    [self.view addConstraints:constraints];
}

谢谢!

-- 更新 - 问题似乎出在 [self.view removeConstraints:self.view.constraints]; 如果我将其注释掉,它工作正常。不知道为什么。

【问题讨论】:

  • 黑条表示您没有 4" 启动图像。

标签: ios objective-c autolayout


【解决方案1】:

尝试拨打[self.view setNeedsUpdateConstraints]。您正在调用[self.view needsUpdateConstraints],它只是询问是否已调用setNeedsUpdateConstraints

【讨论】:

  • 我删除了更改updateViewConstraints名称的错误指令。
  • 问题似乎出在 [self.view removeConstraints:self.view.constraints];在 updateViewContraints 方法中。专门针对 iOS 8。如果我将其注释掉,我不会收到模棱两可的布局警告
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多