gpt4 book ai didi

ios - 以编程方式设置自动布局约束

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:08 25 4
gpt4 key购买 nike

我不知道如何解决这个问题:我想创建一个比赛游戏,其中卡片的大小取决于设备。

我想我会创建一个 contentView,我将其固定到 rootView 的顶部和底部,边距为 20,并将其对齐到它的中心。然后我给它一个 3/4 的纵横比(3 行 4 列)。

let contentView = UIView()
// place contentView on the view
self.view.addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false

//add position constraints to thecontentView
let topMargin = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20)
let bottomMargin = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -20)

let horzontalAllignment = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
let verticalAllignment = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)

self.view.addConstraints([topMargin, bottomMargin, horzontalAllignment, verticalAllignment])

// create an aspect ratio for the contentView
let aspect = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Height, multiplier: 4/3, constant: 0)
contentView.addConstraint(aspect)

这很完美。 (哇哦!)然后我想创建一张卡片,它的宽度是 contentView 的四分之一,高度是三分之一:

let thisCard = Card()
contentView.addSubview(thisCard)
thisCard.translatesAutoresizingMaskIntoConstraints = false

let hightConstraint = NSLayoutConstraint(item: thisCard, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Height, multiplier: 1/3, constant: 0)

let widthConstraint = NSLayoutConstraint(item: thisCard, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1/4, constant: 0)

thisCard.addConstraints([hightConstraint, widthConstraint])

代码中断,我收到消息:

The view hierarchy is not prepared for the constraint: NSLayoutConstraint:0x78f1b290 Meeres_Memo_temp_caseinsensitive_renamery.Card:0x78f41820.height == UIView:0x78f49fc0.height When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled.

有没有可能,做我想做的事?

更新:

我刚刚发现我的错误。我将 heightConstraint 和 widthConstraint 添加到 thisCard,但它们需要添加到 View 层次结构中较高的相关 View ,在本例中是 contentView,如下所示:

contentView.addConstraints([hightConstraint, widthConstraint])

最佳答案

阅读错误信息。如果项目位于同一 View 层次结构中,则只能添加约束。我认为您在将 thisCard 或 contentView 添加到 View 层次结构之前尝试添加约束。你需要先这样做。

顺便说一句。 1/4 不是你想的那样——它是一个大胖零。 1/4 使用整数除法。例如,使用 1.0/4。

关于ios - 以编程方式设置自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831295/

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