【问题标题】:Constraints programmatically do not work the same way as constraints in storyboard约束以编程方式的工作方式与情节提要中的约束不同
【发布时间】:2018-10-20 16:30:43
【问题描述】:

我正在尝试以编程方式使用自动布局来使我的自定义视图在边界发生变化时适应其大小。 我想要达到的目标:
1) 使视图在屏幕上尽可能大,但保持其高度和大小之间的纵横比为 8/5
2) 永远不要走出安全区
3) 永远停留在中间
当我在情节提要中使用这组约束时,一切正常,但是当我在代码中执行相同操作时,xcode 会破坏我的高度约束(我将其用于视图的纵横比)。我玩过优先级但没有成功。我究竟做错了什么? 这是我的代码和我的故事板约束的屏幕截图:

screenshot of storyboard constraints

private func setupLayout () {

    playingCardView.translatesAutoresizingMaskIntoConstraints = false

    //makes the maxim width possible
    let playingCardViewWidthConstraint = playingCardView.widthAnchor.constraint(equalToConstant: 800)
    playingCardViewWidthConstraint.priority = UILayoutPriority(rawValue: 250)
    playingCardViewWidthConstraint.identifier = "width"

    //for aspect ratio
    let playingCardViewHeightConstraint = playingCardView.heightAnchor.constraint(equalTo: playingCardView.widthAnchor, multiplier: 8.0/5.0)
    playingCardViewHeightConstraint.identifier = "height"

    //make the view stay within bounds
    //add some padding top
    let playingCardTopConstraint = playingCardView.topAnchor.constraint(lessThanOrEqualTo: view.safeAreaLayoutGuide.topAnchor, constant: Constants.offsetFromTheEdge)

    //add some padding bottom
    let plaingCardViewBottomConstraint = playingCardView.bottomAnchor.constraint(lessThanOrEqualTo: view.safeAreaLayoutGuide.bottomAnchor, constant: Constants.offsetFromTheEdge)

    //add some padding leading
    let playingCardViewLeadingConstraint = playingCardView.leadingAnchor.constraint(lessThanOrEqualTo: view.layoutMarginsGuide.leadingAnchor, constant: Constants.offsetFromTheEdge)

    ////add some padding trailing
    let playingCardViewTrailingConstraint = playingCardView.trailingAnchor.constraint(lessThanOrEqualTo: view.layoutMarginsGuide.trailingAnchor, constant: Constants.offsetFromTheEdge)

    NSLayoutConstraint.activate([
        playingCardViewWidthConstraint,
        playingCardViewHeightConstraint,
        playingCardView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        playingCardView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        playingCardTopConstraint,
        playingCardViewBottomConstraint,
        playingCardViewLeadingConstraint,
        playingCardViewTrailingConstraint
            ])
    }
}

【问题讨论】:

  • 您是否使用 Xcode 的 view hierarchy debugging tool 检查过您的任一设置?
  • @PaulPatterson 是的,我做到了。 Xcode 打破了我的高度限制,并添加了一个 1.6 纵横比的新限制。问题是我只是在学习编程,所以我可能会错过一些重要的东西。

标签: ios swift autolayout constraints programmatically


【解决方案1】:

当涉及到约束时,您需要考虑 4 件事

x => 前导/尾随/左/右/centerX

y => 顶部、底部、中心Y

宽度 => 静态/比例

高度 => 静态/比例

我认为在这里你会触发所有可能的冲突,无论优先级如何

您将视图的宽度设置为 800 并为其设置前导 && 尾随肯定小于该宽度,如何??

你给视图纵横比高度并钩它的顶部和底部,如何??

你给出前导和尾随并给出 centerX ,为什么要??

你给 top 和 bottom 并给 centerY ,为什么要??

如果不会添加任何值,请不要添加约束

【讨论】:

  • 我真正不明白的是,为什么当我们将这组约束添加到情节提要没有错误时它会起作用? (我用斯坦福大学课程的逻辑工具)为什么相同的逻辑有不同的结果?
【解决方案2】:

在您发布的代码中...

您在let plaingCardViewBottomConstraint = ... 中缺少y

除此之外,您将顶部和底部约束设置为 view.safeAreaLayoutGuide,但您将前导和尾随约束设置为 layoutMarginsGuide

view.layoutMarginsGuide 更改为view.safeAreaLayoutGuide,你应该得到你所期望的。

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2017-03-27
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多