手动添加约束的问题,“同时满意”

时间:2016-01-24 23:29:09

标签: ios

我一直在我的应用程序中使用自动布局,并且在此之前使用界面构建器来完成所有这些,因为我需要根据某些方面动态更改我的界面,这意味着我必须修改约束的值,并在视图中添加/删除元素。

我知道错误“无法同时满足约束”意味着什么,但我不知道问题是什么。此外,显示错误的代码对我来说看起来像一个外语,我甚至不知道哪些约束是冲突的。我只添加了4个约束

  • 的centerX
  • 宽度
  • 高度

通过界面构建​​器添加这些不会导致问题,所以我对这里的问题感到有些困惑,所以这是我的代码:

func setupProfilePicture() {

        profilePictureView = UIImageView()
        profilePictureView.layer.cornerRadius = profilePictureView.bounds.width / 2
        profilePictureView.clipsToBounds = true
        profilePictureView.layer.borderWidth = 2;
        profilePictureView.layer.borderColor = UIColor.whiteColor().CGColor
        profilePictureView.contentMode = .ScaleAspectFill

        profileConstraints = [NSLayoutConstraint]();

        // HORIZONTAL CENTER
        profileConstraints.append(
            NSLayoutConstraint(item: profilePictureView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)
        )
        // FROM TOP
        profileConstraints.append(
            NSLayoutConstraint(item: profilePictureView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 100)
        )
        // WIDTH
        profileConstraints.append(
            NSLayoutConstraint(item: profilePictureView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 150)
        )
        // HEIGHT
        profileConstraints.append(
            NSLayoutConstraint(item: profilePictureView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 150)
        )


        view.addSubview(profilePictureView)

        NSLayoutConstraint.activateConstraints(profileConstraints);

    }

这是我收到的错误:

(
    "<NSAutoresizingMaskLayoutConstraint:0x7ff718dc9b70 h=--& v=--& UIImageView:0x7ff718e9f170.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x7ff718dc9bc0 h=--& v=--& V:[UIImageView:0x7ff718e9f170(0)]>",
    "<NSLayoutConstraint:0x7ff718e9f7e0 V:|-(100)-[UIImageView:0x7ff718e9f170]   (Names: '|':UIView:0x7ff718dc0840 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7ff718dca490 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7ff718dc0840]   (Names: '|':_UIParallaxDimmingView:0x7ff718dbe050 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff718e9f7e0 V:|-(100)-[UIImageView:0x7ff718e9f170]   (Names: '|':UIView:0x7ff718dc0840 )>

0 个答案:

没有答案