Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS上的制图:设置动画时不遵守CenterX约束_Ios_Autolayout_Ios Autolayout_Cartography - Fatal编程技术网

iOS上的制图:设置动画时不遵守CenterX约束

iOS上的制图:设置动画时不遵守CenterX约束,ios,autolayout,ios-autolayout,cartography,Ios,Autolayout,Ios Autolayout,Cartography,我正在尝试使用 使用此代码: let group = ConstraintGroup() constrain(alertLabel, replace: group) { alertLabel in alertLabel.centerX == alertLabel.superview!.centerX alertLabel.width == alertLabel.superview!.width * Constants.alertLabelWid

我正在尝试使用 使用此代码:

    let group = ConstraintGroup()

    constrain(alertLabel, replace: group) { alertLabel in
        alertLabel.centerX  == alertLabel.superview!.centerX
        alertLabel.width == alertLabel.superview!.width * Constants.alertLabelWidthMultiplier
        alertLabel.bottom == alertLabel.superview!.top + 0
    }

    constrain(alertLabel, replace: group) { alertLabel in
        alertLabel.centerX  == alertLabel.superview!.centerX
        alertLabel.width == alertLabel.superview!.width * Constants.alertLabelWidthMultiplier
        alertLabel.bottom == alertLabel.superview!.top + 60
    }

    UIView.animateWithDuration(0.5, animations: alertLabel.layoutIfNeeded)

我希望我的UILabel在动画的开头和结尾居中。 但是,它似乎从superview的左上角开始。
我在这里做错了什么?

问题在于这行:

UIView.animateWithDuration(0.5, animations: alertLabel.layoutIfNeeded)
应该是:

UIView.animateWithDuration(0.5, animations: alertLabel.superview!.layoutIfNeeded)

centerX关系NSLayoutConstraint位于superview上-因此需要调用LayoutIfNeed的是superview

问题在于这一行:

UIView.animateWithDuration(0.5, animations: alertLabel.layoutIfNeeded)
应该是:

UIView.animateWithDuration(0.5, animations: alertLabel.superview!.layoutIfNeeded)
centerX关系NSLayoutConstraint位于superview上-因此需要调用LayoutIfNeed的是superview