gpt4 book ai didi

ios - 自动布局约束行为不正确?

转载 作者:行者123 更新时间:2023-11-30 12:24:19 24 4
gpt4 key购买 nike

因此,我创建的 View 有一个非常简单的布局,基本上在添加约束后我希望它看起来如下,

enter image description here

但是,我最终得到的是别的东西,而且我不确定为什么会发生这种行为。

enter image description here

所以,我只是快速概述一下我为实现我想要的布局所做的工作。

  1. "/" 标签的 X 位置居中
  2. 设置可以在左侧看到的“1”的indexLabel
  3. “reps”标签限制在“/”标签左侧
  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
}

最佳答案

为了让 anchor 正常工作,您应该添加_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

}

关于ios - 自动布局约束行为不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com