Ios 以编程方式在水平stackView中使用UIImageView和UILabel时无法同时满足约束

Ios 以编程方式在水平stackView中使用UIImageView和UILabel时无法同时满足约束,ios,objective-c,swift,xcode7,Ios,Objective C,Swift,Xcode7,我有以下代码 class SecondViewController: UIViewController { let kFontName = "SourceSansPro-Light" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let firstRowTextLabel

我有以下代码

class SecondViewController: UIViewController {
let kFontName = "SourceSansPro-Light"
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let firstRowTextLabel               = UILabel()
        firstRowTextLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
        firstRowTextLabel.heightAnchor.constraintEqualToConstant(20.0).active = true
        firstRowTextLabel.text  = "Hi World"
        firstRowTextLabel.font = UIFont(name:kFontName, size:24)
        // textLabel.textAlignment = .Center
        firstRowTextLabel.translatesAutoresizingMaskIntoConstraints = false

        let firstRowImageView               = UIImageView()
        firstRowImageView.heightAnchor.constraintEqualToConstant(60.0).active = true
        firstRowImageView.widthAnchor.constraintEqualToConstant(60.0).active = true
        firstRowImageView.image = UIImage(named: "settings_black")
        firstRowImageView.translatesAutoresizingMaskIntoConstraints = false

        let firstRowStackView   = UIStackView()
        firstRowStackView.axis  = UILayoutConstraintAxis.Horizontal
        firstRowStackView.distribution = UIStackViewDistribution.FillEqually
        firstRowStackView.alignment = UIStackViewAlignment.Center
        firstRowStackView.spacing = 25.0

        firstRowStackView.addArrangedSubview(firstRowImageView)
        firstRowStackView.addArrangedSubview(firstRowTextLabel)
        firstRowStackView.translatesAutoresizingMaskIntoConstraints = false


        let secondRowLabel = UILabel()
        secondRowLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
        secondRowLabel.heightAnchor.constraintEqualToConstant(20.0).active = true


        let stackView   = UIStackView()
        stackView.axis  = UILayoutConstraintAxis.Vertical
        stackView.distribution  = UIStackViewDistribution.EqualSpacing
        stackView.alignment = UIStackViewAlignment.Center
        stackView.spacing   = 25.0


        stackView.addArrangedSubview(firstRowStackView)
        stackView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(stackView)


        stackView.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor).active = true

        let constraint = NSLayoutConstraint(
            item: stackView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: topLayoutGuide,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 50.0
        )
        self.view.addConstraint(constraint)
    }
...
}
当我运行它时,我得到以下结果:

这是我想要的布局,除了我得到了

无法同时满足约束

我的控制台中的消息

当我注释掉以下行时:

firstRowStackView.distribution = UIStackViewDistribution.FillEqually
firstRowTextLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
 // firstRowStackView.distribution = UIStackViewDistribution.FillEqually
我的控制台中的打破约束错误消失了,但布局不再对齐图像和文本中心,取而代之的是:

为了在不破坏约束的情况下实现以下布局,我需要对上述代码进行的最小更改是什么:


好的。我刚刚想出了解决这个问题的办法

我更改了以下行:

firstRowStackView.distribution = UIStackViewDistribution.FillEqually
firstRowTextLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
 // firstRowStackView.distribution = UIStackViewDistribution.FillEqually

并注释掉以下行:

firstRowStackView.distribution = UIStackViewDistribution.FillEqually
firstRowTextLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true
 // firstRowStackView.distribution = UIStackViewDistribution.FillEqually
这对我来说是个好办法