2017-06-06 156 views
0

所以我有一個非常簡單的佈局,我已經創建了一個觀點,基本上我希望它看起來像我跟添加約束如下完成後,自動佈局約束不正確?

enter image description here

不過,我最終是另一回事,我不知道爲什麼會發生這種行爲。

enter image description here

所以正好與我在試圖達到我想要的佈局做了簡要總結。

  1. 中心的X位置「/」標籤
  2. 設置的indexLabel,可以實現這一說,左側可以看到「1」
  3. 約束的「代表」標籤「/」標籤
  4. 最後將「代表字段」限制在的右邊indexLabel和左「銷售代表」 的標籤

private func setupDividerLabelLayout() { 
    addSubview(dividerLabel) 

    dividerLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    dividerLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true 
} 

private func setupIndexLabelBackgroundLayout() { 
    addSubview(indexLabelBackground) 

    indexLabelBackground.leftAnchor.constraint(equalTo: leftAnchor, constant: 24).isActive = true 
    indexLabelBackground.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    indexLabelBackground.heightAnchor.constraint(equalToConstant: 24).isActive = true 
    indexLabelBackground.widthAnchor.constraint(equalToConstant: 24).isActive = true 

} 

private func setupRepsLabelLayout() { 
    addSubview(repsLabel) 

    repsLabel.rightAnchor.constraint(equalTo: dividerLabel.leftAnchor, constant: -16).isActive = true 
    repsLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
} 

private func setupRepsFieldLayout() { 
    addSubview(repsField) 

    repsField.rightAnchor.constraint(equalTo: repsLabel.leftAnchor, constant: -8).isActive = true 
    repsField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    repsField.leftAnchor.constraint(equalTo: indexLabelBackground.rightAnchor, constant: 8).isActive = true 
} 
+1

,不從問題中移除的最後幾行。 (如:[1]:https://i.stack.imgur.com/......jpg) – wajeeh

+0

只是注意到編輯 –

+0

使用堆棧視圖,而不是搞亂自動佈局約束手動 –

回答

0

爲了讓你的工作錨你應該增加_ViewName_.translatesAutoresizingMaskIntoConstraints = false
,使代碼變成如下
代碼,當你在你的問題分享圖片使用

private func setupDividerLabelLayout() { 
    addSubview(dividerLabel) 

    dividerLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    dividerLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true 
    dividerLabel.translatesAutoresizingMaskIntoConstraints = false 
} 

private func setupIndexLabelBackgroundLayout() { 
    addSubview(indexLabelBackground) 

    indexLabelBackground.leftAnchor.constraint(equalTo: leftAnchor, constant: 24).isActive = true 
    indexLabelBackground.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    indexLabelBackground.heightAnchor.constraint(equalToConstant: 24).isActive = true 
    indexLabelBackground.widthAnchor.constraint(equalToConstant: 24).isActive = true 
    indexLabelBackground.translatesAutoresizingMaskIntoConstraints = false 

} 

private func setupRepsLabelLayout() { 
    addSubview(repsLabel) 

    repsLabel.rightAnchor.constraint(equalTo: dividerLabel.leftAnchor, constant: -16).isActive = true 
    repsLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    repsLabel.translatesAutoresizingMaskIntoConstraints = false 

} 

private func setupRepsFieldLayout() { 
    addSubview(repsField) 

    repsField.rightAnchor.constraint(equalTo: repsLabel.leftAnchor, constant: -8).isActive = true 
    repsField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    repsField.leftAnchor.constraint(equalTo: 
    indexLabelBackground.rightAnchor, constant: 8).isActive = true 
    repsField.translatesAutoresizingMaskIntoConstraints = false 
    indexLabelBackground.translatesAutoresizingMaskIntoConstraints = false 

} 
+0

這已經完成,它只是被省略,對於混淆抱歉 –