我有一些在iOS 7中可以使用的自动布局代码,但在iOS 8中却没有。我的代码在两个版本之间没有更改。

情况是,我有一个视图控制器,其中包含一些子视图,这些子视图应垂直堆叠,如下所示:

但是,当我在iOS 8上运行相同的代码时,会发生以下情况:

设置我的约束的代码如下:

- (void) updateViewConstraints {
    [super updateViewConstraints];

    NSDictionary *viewsDictionary = @{
        @"timeLabel": self.timeLabel,
        @"recordingStateImage": self.recordingStateImage,
        @"recordButton": self.recordButton,
        @"spacer1": spacer1,
        @"spacer2": spacer2,
        @"superview": self.view
    };

    if (!portraitConstraints) {
        //
        // Portrait Layout
        //

        portraitConstraints = [[NSMutableArray alloc] initWithArray:
            [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[timeLabel][spacer1][recordingStateImage][spacer2(==spacer1)][recordButton(100)]-|"
                                                    options:0
                                                    metrics:0
                                                      views:viewsDictionary]];

        [portraitConstraints addObjectsFromArray:@[
            [NSLayoutConstraint constraintWithItem:self.timeLabel
                                         attribute:NSLayoutAttributeCenterX
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:self.view
                                         attribute:NSLayoutAttributeCenterX
                                        multiplier:1.f
                                          constant:0.f],
            [NSLayoutConstraint constraintWithItem:self.recordingStateImage
                                         attribute:NSLayoutAttributeCenterX
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:self.view
                                         attribute:NSLayoutAttributeCenterX
                                        multiplier:1.f
                                          constant:0.f],
            [NSLayoutConstraint constraintWithItem:self.recordButton
                                         attribute:NSLayoutAttributeCenterX
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:self.view
                                         attribute:NSLayoutAttributeCenterX
                                        multiplier:1.f
                                          constant:0.f]
        ]];
    }

    [self.view removeConstraints:self.view.constraints];
    [self.view addConstraints:portraitConstraints];

}

我不确定为什么会这样。我已经检查了以下SO问题中指出的解决方法:
  • Autolayout problems with iOS8 with code that works fine on iOS7
  • Issue with Auto Layout on iOS 8 (code works perfectly on iOS 7)

  • 但它们没有起作用(这是在子视图而不是视图控制器的视图上调用setNeedsLayout)。

    如果有人在iOS 8上遇到NSLayoutAttributeCenterX和自动布局问题,将不胜感激。

    最佳答案

    ,因为我的评论有助于解决问题,OP需要有关同一问题的更多信息,所以我将评论发布为答案

    注意:
    删除所有约束是不好的做法,因为它将删除XCode添加的所有约束以使您的视图正确地适合设备。

    在横向和纵向模式下具有不同的UI
    如果您的视图在横向和纵向上具有不同的约束(在紧凑和常规视图中)。
    您可以将大小类introduce in XCode6backward comparability一起使用。

    请按照此blog很好地解释size类。
    download project在以上博客中创建。

    输出:
    以风景模式查看

    以纵向模式查看:

    关于ios - iOS 8:为什么NSLayoutAttributeCenterX无法使 View 居中? (适用于iOS 7),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27285606/

    10-11 19:23
    查看更多