gpt4 book ai didi

ios - 使用自动布局将 subview 添加到另一个 subview

转载 作者:行者123 更新时间:2023-11-30 11:03:50 26 4
gpt4 key购买 nike

我确信我在这里错过了一些愚蠢的东西,但我有 mainView、subviewA 和 subviewB。我试图将 subviewB 添加到 subviewA 并将其锚定在 subviewA 内部,但是它没有锚定(只是保留在左上角。但是,如果我将 subviewB 添加到 mainView 然后锚定它,它工作正常。

示例(使用我认为不言自明的自定义锚定函数):

addSubview(questionContainerView)
questionContainerView.anchor(topAnchor, left: leftAnchor, bottom: centerYAnchor, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)


// does not work
questionContainerView.addSubview(questionTextLabel)
questionTextLabel.anchor(questionContainerView.topAnchor, left: questionContainerView.leftAnchor, bottom: questionContainerView.bottomAnchor, right: questionContainerView.rightAnchor, topConstant: 25, leftConstant: 10, bottomConstant: 25, rightConstant: 10, widthConstant: 0, heightConstant: 0)

// does work
addSubview(questionTextLabel)
questionTextLabel.anchor(questionContainerView.topAnchor, left: questionContainerView.leftAnchor, bottom: questionContainerView.bottomAnchor, right: questionContainerView.rightAnchor, topConstant: 25, leftConstant: 10, bottomConstant: 25, rightConstant: 10, widthConstant: 0, heightConstant: 0)

最佳答案

我尝试了相同的代码,使用不同的背景颜色,没有发现任何问题,也许是因为相同的背景颜色,它不会可见。

请找到下面的代码

        self.view.addSubview(questionContainerView)
questionContainerView.anchor(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.centerYAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
questionContainerView.backgroundColor = .blue

questionContainerView.addSubview(questionTextLabel)
questionTextLabel.anchor(top: questionContainerView.topAnchor, left: questionContainerView.leftAnchor, bottom: questionContainerView.bottomAnchor, right: questionContainerView.rightAnchor, topConstant: 25, leftConstant: 10, bottomConstant: 25, rightConstant: 10, widthConstant: 0, heightConstant: 0)
questionTextLabel.backgroundColor = .black

please find the screenshot attached

关于ios - 使用自动布局将 subview 添加到另一个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52982306/

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