简体   繁体   English

更改NSLayoutConstraint在iOS7中不起作用

[英]Changing NSLayoutConstraint is not working in iOS7

I have this code in viewDidLoad 我在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;

which will adjust the panel to centre the screen. 这将调整面板以使屏幕居中。 Everything works fine i get the correct adjustHeight value for both iOS6 and iOS7 一切正常,我为iOS6和iOS7获得了正确的adjustHeight

but when i set the constant on iOS7 it is not updating. 但是当我在iOS7上设置常量时,它没有更新。 The layout still the same. 布局仍然相同。

I don't know what will cause this because i have another pages which use the same piece of this code and that pages don't have a problem like this. 我不知道是什么原因造成的,因为我还有另一个页面使用了这段代码,并且这些页面没有这样的问题。

Any idea what can cause this problem and how can i solve it? 任何想法会导致此问题,我该如何解决? Thanks. 谢谢。

Most probably, for the changes to populate you need to add 最可能的是,要填充更改,您需要添加

[self.view setNeedsLayout];
[self.view layoutIfNeeded];

However, it isn't the correct way to align the view to the centre. 但是,这不是将视图与中心对齐的正确方法。 Correct way is adding a centring constraint, either in Interface Builder: 正确的方法是在Interface Builder中添加居中约束:

在此处输入图片说明

or in the code: 或在代码中:

// 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]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM