gpt4 book ai didi

ios - 为什么约束在 UITableView 滚动时更新?

转载 作者:行者123 更新时间:2023-11-28 10:35:00 24 4
gpt4 key购买 nike

我有 2 个简单的 UILabel 需要水平放置。其中之一在 View 的右侧,而第二个在左侧并获得所有可能的空间。这是我的约束条件:

    amountOfQuestions.rightAnchor.constraint(equalTo: rightAnchor, constant: -8).isActive = true
amountOfQuestions.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true


topicName.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
topicName.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
topicName.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8).isActive = true
topicName.rightAnchor.constraint(equalTo: amountOfQuestions.leftAnchor, constant: -8).isActive = true

看起来一切正常,但是当显示 TableView 时,它看起来像这样:

enter image description here

但是在我上下滚动表格几次后,它变得正常了:

enter image description here

为什么我的表格没有像第二张图片那样立即显示出来?

解决方案:

amountOfQuestions.rightAnchor.constraint(equalTo: rightAnchor, constant: -8).isActive = true
amountOfQuestions.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
amountOfQuestions.setContentHuggingPriority(.required, for: .horizontal)
amountOfQuestions.setContentCompressionResistancePriority(.required, for: .horizontal)

topicName.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
topicName.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
topicName.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8).isActive = true
topicName.rightAnchor.constraint(equalTo: amountOfQuestions.leftAnchor, constant: -8).isActive = true
topicName.setContentHuggingPriority(.required, for: .horizontal)
topicName.setContentCompressionResistancePriority(.required, for: .vertical)


let myBottom = topicName.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
myBottom.priority = UILayoutPriority(rawValue: UILayoutPriority.required.rawValue - 1)
myBottom.isActive = true

最佳答案

有多个问题会影响布局并使其定义不正确:

  1. 切勿将内容限制在单元格本身。始终约束到单元格的 contentView,例如:

    amountOfQuestions.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8).isActive = true

(确保将标签添加为 contentView 的 subview ,而不是单元格本身)。

  1. 如果您希望标签的大小产生任何影响,您需要正确设置拥抱和压缩阻力优先级。否则布局不必尊重内容:

    // you want topic name to resize vertically to respect the content
    topicName.setHuggingPriority(.required, for: .vertical)
    topicName.setContentCompressionResistancePriority(.required, for: .vertical)

    // you want amount of question to resize horizontally to respect the content
    amountOfQuestions.setHuggingPriority(.required, for: .horizontal)
    amountOfQuestions.setContentCompressionResistancePriority(.required, for: .horizontal)

  1. 为避免在第一个布局期间发生布局冲突,最好为其中一个垂直约束赋予稍低的布局优先级:

    let bottomAnchor = topicName.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8)
    bottomAnchor.priority = UILayoutPriority(rawValue: UILayoutPriority.required.rawValue - 1)
    bottomAnchor.isActive = true

关于ios - 为什么约束在 UITableView 滚动时更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53960374/

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