gpt4 book ai didi

swift - 如何修复高度约束和中心 Y 约束之间的 'Unable to simultaneously satisfy constraints.' 冲突

转载 作者:行者123 更新时间:2023-11-28 11:39:38 29 4
gpt4 key购买 nike

我想使 UILabel 在 UIView 中垂直居中。我正在使用以下约束:

layoutGuide = safeAreaLayoutGuide

header.translatesAutoresizingMaskIntoConstraints = false

titleLabel.translatesAutoresizingMaskIntoConstraints = false

header.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
header.rightAnchor.constraint(equalTo: layoutGuide.rightAnchor).isActive = true
header.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
header.heightAnchor.constraint(equalToConstant: UI.HEADER_HT).isActive = true


titleLabel.topAnchor.constraint(equalTo: header.topAnchor, constant: SPACING.LG).isActive = true
titleLabel.rightAnchor.constraint(equalTo: header.rightAnchor, constant: -SPACING.LG).isActive = true
titleLabel.leftAnchor.constraint(equalTo: header.leftAnchor, constant: SPACING.LG).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: UI.SCREEN_TITLE_HT).isActive = true
titleLabel.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true

标签正确居中,但未遵守高度限制

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000003ade50 FC.StyledLabel:0x7fa077845330.height == 40.6 (active)>

最佳答案

如果您希望标题垂直居中,您可能不想同时使用 topAnchor 和 centerYAnchor 约束。也许您想将该 header 锚定到标签的顶部?

另外,检查 NSLayoutConstraint.activate。它将允许您使用所有 isActive = true

关于swift - 如何修复高度约束和中心 Y 约束之间的 'Unable to simultaneously satisfy constraints.' 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045290/

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