NSLayoutConstraint常量未更新

时间:2017-05-09 16:41:51

标签: ios iphone swift autolayout constraints

如果设备是iPhone SE,我想更新约束。

我有一个xib视图控制器,我将其视图添加为子视图。

    //get the empty view
    let emptyVC = JobsEmptyStateView(nibName: "JobsEmptyStateView", bundle: nil)
    let emptyView = emptyVC.view!
    emptyView.frame = self.view.frame

    self.navigationController?.view.insertSubview(emptyView, belowSubview: (self.navigationController?.navigationBar)!)

在JobsEmptyStateView中我有

@IBOutlet weak var headerLabelTopLayout: NSLayoutConstraint!

我也尝试将其设置为private var

enter image description here

enter image description here

最后我有一个类决定该设备是否是iPhoneSE

class func emptyStateHeaderLabelTopLayout() -> CGFloat? {

    let device = Device()

    if device.isOneOf(smallScreenDevices) {
        if Constants.isDevicePortrait()
        {
            return 200
        }
        return 200

    }

    return nil
}

在JobsEmptyStateView中:

override func viewDidLoad() {

        if let topConstant = Constants.emptyStateHeaderLabelTopLayout()
        {
                //self.headerLabelTopLayout.isActive = true
                print(self.headerLabelTopLayout.constant)
                print(topConstant)
                self.headerLabelTopLayout.constant = topConstant
                print(self.headerLabelTopLayout.constant)
                self.view.setNeedsUpdateConstraints()
                self.view.layoutIfNeeded()
                print(self.headerLabelTopLayout.constant)

        }

    }

打印:

70.0
200.0
200.0
70.0

为什么约束没有得到更新?

1 个答案:

答案 0 :(得分:2)

尝试删除wR hC约束,只允许值为70