更改NSLayoutConstraint在iOS7中不起作用

更改NSLayoutConstraint在iOS7中不起作用,ios,autolayout,Ios,Autolayout,我在viewDidLoad int height = 367 - self.vLayout.frame.size.height; int adjustHeight = height / 2; self.csPanelBottom.constant = adjustHeight - 2; if (height % 2 != 0) { adjustHeight += 1; } self.csPan

我在
viewDidLoad

int height = 367 - self.vLayout.frame.size.height;
        int adjustHeight = height / 2;
        self.csPanelBottom.constant = adjustHeight - 2;
        if (height % 2 != 0) {
            adjustHeight += 1;
        }
        self.csPanelTop.constant = adjustHeight + 2;
这将调整面板以使屏幕居中。 一切正常我得到了iOS6和iOS7的正确
adjustHeight

但当我在iOS7上设置常量时,它并没有更新。布局还是一样的

我不知道是什么原因导致了这一点,因为我有另一个页面使用了同一段代码,而这些页面没有这样的问题

你知道什么会导致这个问题吗?我该如何解决?
谢谢。

最有可能的是,为了填充您需要添加的更改

[self.view setNeedsLayout];
[self.view layoutIfNeeded];
但是,将视图与中心对齐不是正确的方法。正确的方法是在Interface Builder中添加居中约束:

或在代码中:

// Replace viewNeedingCenter and containerView with your actual views
[containerView addConstraint:
 [NSLayoutConstraint constraintWithItem:viewNeedingCenter
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:containerView
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1.0 constant:0]];

很可能,要填充更改,您需要添加

[self.view setNeedsLayout];
[self.view layoutIfNeeded];
但是,将视图与中心对齐不是正确的方法。正确的方法是在Interface Builder中添加居中约束:

或在代码中:

// Replace viewNeedingCenter and containerView with your actual views
[containerView addConstraint:
 [NSLayoutConstraint constraintWithItem:viewNeedingCenter
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:containerView
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1.0 constant:0]];

更改常量后是否调用[self-setNeedsLayout]?不,我在任何页面中都不会调用它,所以您应该尝试一下。在设置常数后添加[self-setNeedsLayout]。它似乎不起作用。更改常数后是否调用[self-setNeedsLayout]?不,我在任何页面中都不调用它,所以您应该尝试一下。在设置常量后添加[self setNeedsLayout]。它似乎不起作用。