移动parentView时出现约束错误

时间:2016-06-16 22:46:14

标签: ios swift animation nslayoutconstraint

我使用以下约束设置了视图:

 bottomContainerViewConstraint = NSLayoutConstraint(item: containerView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -10)
    self.view.addConstraint(bottomContainerViewConstraint!)
    topContainerViewConstraint = NSLayoutConstraint(item: containerView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 10)
    self.view.addConstraint(topContainerViewConstraint!)
    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: -10))
    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -120))

    //caption and profilepic constraints

    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: -110))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0))

    self.view.addConstraintsWithFormat("H:|-8-[v0(50)]", views: profilePicImageView)
    self.view.addConstraintsWithFormat("V:|-30-[v0(50)]", views: profilePicImageView)
    profilePicImageView.layer.cornerRadius = 25
    profilePicImageView.layer.masksToBounds = true



    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Top, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Leading, relatedBy: .Equal, toItem: profilePicImageView, attribute: .Trailing, multiplier: 1, constant: 8))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Trailing, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Bottom, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Bottom, multiplier: 1, constant: 0))

    //likes and comments and divider


    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Leading, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0))


    self.view.addConstraint(NSLayoutConstraint(item: voteUpButton, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: voteUpButton, attribute: .Leading, relatedBy: .Equal, toItem: commentButton, attribute: .Trailing, multiplier: 1, constant: 0))


    self.view.addConstraint(NSLayoutConstraint(item: likesLabel, attribute: .Top, relatedBy: .Equal, toItem: voteUpButton, attribute: .Bottom, multiplier: 1, constant: -5.5))
    self.view.addConstraint(NSLayoutConstraint(item: likesLabel, attribute: .Leading, relatedBy: .Equal, toItem: voteUpButton, attribute: .Leading, multiplier: 1, constant: 11.5))

    self.view.addConstraint(NSLayoutConstraint(item: voteDownButton, attribute: .Top, relatedBy: .Equal, toItem: likesLabel, attribute: .Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: voteDownButton, attribute: .Leading, relatedBy: .Equal, toItem: likesLabel, attribute: .Leading, multiplier: 1, constant: -11.5))

    self.view.addConstraint(NSLayoutConstraint(item: commentButton, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 37))
    self.view.addConstraint(NSLayoutConstraint(item: commentButton, attribute: .Leading, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Leading, multiplier: 1, constant: 10))

    self.view.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: .Top, relatedBy: .Equal, toItem: commentButton, attribute: .Top, multiplier: 1, constant: 7.5))
    self.view.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: .Leading, relatedBy: .Equal, toItem: commentButton, attribute: .Leading, multiplier: 1, constant: 12.5))

现在我正在尝试更改bottomContainerViewConstrainttopContainerViewConstraint,并使用以下代码将containerView位置移动到屏幕顶部:

func moveViewFromBottomToTop(viewToBeMoved : UIView, bottomConstraint : NSLayoutConstraint, topConstraint : NSLayoutConstraint, completionHandler: (loaded: Bool, dataNil: Bool) -> ()) -> () {
    if (topConstraint.constant >  (-self.view.frame.height)) {
        topConstraint.constant = -self.view.frame.height
        bottomConstraint.constant = -self.view.frame.height + 120
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            self.view.layoutIfNeeded()

            }, completion: { (completed) in


        })
    }

}

代码中提到的所有其他视图都是containerView的子视图。我收到以下错误:

 2016-06-16 23:35:33.282 City[18312:474417] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x7f95e1577e30 UIView:0x7f95e1566670.bottom == UIView:0x7f95e161a710.bottom - 547>",
"<NSLayoutConstraint:0x7f95e159eb40 UIView:0x7f95e1566670.top == UIView:0x7f95e161a710.bottom - 120>",
"<NSLayoutConstraint:0x7f95e15c1160 UIView:0x7f95e15667e0.top == UIView:0x7f95e1566670.top>",
"<NSLayoutConstraint:0x7f95e1502ed0 UIView:0x7f95e15667e0.bottom == UIView:0x7f95e1566670.bottom>",
"<NSLayoutConstraint:0x7f95e15163b0 UITextView:0x7f95e2107c00.top == UIView:0x7f95e15667e0.top>",
"<NSLayoutConstraint:0x7f95e1552b50 UITextView:0x7f95e2107c00.bottom == UIView:0x7f95e15667e0.bottom>"
)

Will attempt to recover by breaking constraint 
 <NSLayoutConstraint:0x7f95e159eb40 UIView:0x7f95e1566670.top == UIView:0x7f95e161a710.bottom - 120>

我不明白为什么这是一个问题。这些约束是否应该自动调整,因为它们都是containerView的子视图,所有约束都是相对于containerView的?

1 个答案:

答案 0 :(得分:0)

不要忘记如果您使用代码约束,则需要禁用translateAutoResizingMaskIntoConstraints

例如:

captionAndProfileImageContainerView.translateAutoresizingMaskIntoConstraints = false
captionTextView.translateAutoresizingMaskIntoConstraints = false

并对所有子视图执行