2015-09-27 28 views
0

我有以下代碼無法使用的UIImageView和的UILabel水平stackView內部時同時滿足約束編程

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) 
    } 
... 
} 

當我運行它,我得到以下結果:

screenshots_horizontal_stackview_1

這是我正在做的佈局,除了我得到的這個事實,我得到了

無法同時滿足的約束

消息在我的控制檯

當我註釋掉以下行:

firstRowStackView.distribution = UIStackViewDistribution.FillEqually 

在我的控制檯斷裂約束錯誤消失但佈局不具備圖像和文字中心對齊了,而是我得到這個:

screenshots_horizontal_stackview_2

什麼是我需要將上面的代碼,爲了不破壞約束來實現以下佈局的最小變化:

screenshots_horizontal_stackview_3

回答

1

確定。我只是想出瞭解決方案。

我改變了以下行:

firstRowTextLabel.widthAnchor.constraintEqualToConstant(self.view.frame.width).active = true 

firstRowTextLabel.widthAnchor.constraintEqualToConstant(100.0).active = true 

和註釋掉以下行:

// firstRowStackView.distribution = UIStackViewDistribution.FillEqually 

這奏效了我。

相關問題