2017-06-06 155 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

这已经完成,它只是被省略,对于混淆抱歉 –