gpt4 book ai didi

ios - 尽管有限制,xcode 按钮仍然重叠

转载 作者:行者123 更新时间:2023-11-29 12:21:51 26 4
gpt4 key购买 nike

我正在使用一系列 4 个按钮。所有相同的宽度和高度组合在一起会拉伸(stretch) View 的宽度。我已经尝试在视觉上将它们彼此约束并限制在它们上方的标签(也是 View 的宽度)。我使用前缘查看最左边按钮上的约束,使用后缘查看最右边按钮上的约束。我还在所有按钮上使用了水平空间限制(应该为零)。尽管如此,当我运行我的模拟器时,第三个按钮总是在某种程度上与第四个按钮重叠。我已经完成并删除了所有约束并尝试再次添加它们并且发生了相同的重叠。我有什么遗漏/做错了吗?为什么我的所有按钮都不会收缩以适应 View 而不是重叠?

最佳答案

我的代码中有类似的东西,如果你以编程方式设置按钮约束,我有一个 ScrollView 和一个内容 View ,并在 Storyboard 中设置约束,但我以编程方式添加其他东西

button1.setTranslatesAutoresizingMaskIntoConstraints(false)

constX = NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)

constTop = NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20)
view.addConstraint(constTop)

constL = NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20)
view.addConstraint(constL)

constR = NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)
view.addConstraint(constR)

constH = NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
button1.addConstraint(constH)


button2.setTranslatesAutoresizingMaskIntoConstraints(false)

constX = NSLayoutConstraint(item: button2, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)

constTop = NSLayoutConstraint(item: button2, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: button1, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10)
view.addConstraint(constTop)

constL = NSLayoutConstraint(item: button2, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20)
view.addConstraint(constL)

constR = NSLayoutConstraint(item: button2, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)
view.addConstraint(constR)

constH = NSLayoutConstraint(item: button2, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
button2.addConstraint(constH)


button3.setTranslatesAutoresizingMaskIntoConstraints(false)

constX = NSLayoutConstraint(item: button3, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)

constL = NSLayoutConstraint(item: button3, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20)
view.addConstraint(constL)

constR = NSLayoutConstraint(item: button3, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)
view.addConstraint(constR)

constTop = NSLayoutConstraint(item: button3, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: button2, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20)
view.addConstraint(constTop)

constH = NSLayoutConstraint(item: button3, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
button3.addConstraint(constH)

button4.setTranslatesAutoresizingMaskIntoConstraints(false)

constX = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)

constTop = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: button3, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10)
view.addConstraint(constTop)

var constBottom = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -20)
view.addConstraint(constBottom)

constL = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 20)
view.addConstraint(constL)

constR = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: ContentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)
view.addConstraint(constR)

constH = NSLayoutConstraint(item: button4, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:50)
button4.addConstraint(constH)

所以有 4 个按钮,它们自己受限,button1.top 到 Contentview.Top 和 button4.Bottom 到 ContentView.Bottom

关于ios - 尽管有限制,xcode 按钮仍然重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407201/

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