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