iOS -AutoLayout constraint set active be crash in second time
iOS -AutoLayout constraint set active be crash in second time
我想实现一个功能,当我点击按钮时,图像会在两个按钮之间移动
我在image和button2(centerX,which text with 222)之间设置了一个constraint,同时我在button1(centerX,which text with 11)也设置了一个constraint,并设置了priority为950,然后拖动它们到 viewcontroller
这是我故事板中的约束
这是我控制约束的代码
if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 {
self.kidLeftCenterConstrait.active = isLeft
self.kidRightCenterConstrait.active = !isLeft
UIView.animateWithDuration(kAnimationDuration, animations: { () -> Void in
self.view.layoutIfNeeded()
})
} else {
let constraintToRemove = isLeft ? self.kidRightCenterConstrait : self.kidLeftCenterConstrait
let constriaintToUse = isLeft ? self.kidLeftCenterConstrait : self.kidRightCenterConstrait
self.douBi.superview!.removeConstraint(constraintToRemove)
self.douBi.superview!.removeConstraint(constriaintToUse)
self.douBi.superview!.addConstraint(constriaintToUse)
UIView.animateWithDuration(kAnimationDuration, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
在 运行 之后 iphone
我点击 button1 图像移动到 11,然后我点击 button2,应用程序是 cash in self.kidRightCenterConstrait.active = !isLeft
控制台日志是 fatal error:unexpectedly found nil while unwrapping an Optional value
看来限制已解除。您可能会尝试强烈保留您的约束:
@IBOutlet var kidLeftCenterConstrait: NSLayoutConstraint!
@IBOutlet var kidRightCenterConstrait: NSLayoutConstraint!
无论如何要注意可能的保留周期。检查您的 ViewController 是否在时间到了时消失。
我通常会在代码中制作这种动画来管理所涉及的约束。
我想实现一个功能,当我点击按钮时,图像会在两个按钮之间移动
我在image和button2(centerX,which text with 222)之间设置了一个constraint,同时我在button1(centerX,which text with 11)也设置了一个constraint,并设置了priority为950,然后拖动它们到 viewcontroller
这是我故事板中的约束
这是我控制约束的代码
if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 {
self.kidLeftCenterConstrait.active = isLeft
self.kidRightCenterConstrait.active = !isLeft
UIView.animateWithDuration(kAnimationDuration, animations: { () -> Void in
self.view.layoutIfNeeded()
})
} else {
let constraintToRemove = isLeft ? self.kidRightCenterConstrait : self.kidLeftCenterConstrait
let constriaintToUse = isLeft ? self.kidLeftCenterConstrait : self.kidRightCenterConstrait
self.douBi.superview!.removeConstraint(constraintToRemove)
self.douBi.superview!.removeConstraint(constriaintToUse)
self.douBi.superview!.addConstraint(constriaintToUse)
UIView.animateWithDuration(kAnimationDuration, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
在 运行 之后 iphone
我点击 button1 图像移动到 11,然后我点击 button2,应用程序是 cash in self.kidRightCenterConstrait.active = !isLeft
控制台日志是 fatal error:unexpectedly found nil while unwrapping an Optional value
看来限制已解除。您可能会尝试强烈保留您的约束:
@IBOutlet var kidLeftCenterConstrait: NSLayoutConstraint!
@IBOutlet var kidRightCenterConstrait: NSLayoutConstraint!
无论如何要注意可能的保留周期。检查您的 ViewController 是否在时间到了时消失。
我通常会在代码中制作这种动画来管理所涉及的约束。