如何向放置在 UIStackView 中的以编程方式创建的 UIButton 添加约束
How do I add constraints to a programmatically created UIButton placed in a UIStackView
我有一个简单的垂直 UIStackView
,在第 3 个子视图中,我想放置以编程方式创建的 UIButton
。我可以这样做,但按钮正在扩展到子视图的整个宽度和高度,而不是我在代码中设置的大小。我使用故事板创建了 UIStackView
,但我以编程方式添加此按钮,因此我可以更好地控制按钮的外观。
let doThisButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
doThisButton.setTitle("Let's do this.", forState: .Normal)
doThisButton.setTitleShadowColor(UIColor.blackColor(), forState: .Highlighted)
doThisButton.translatesAutoresizingMaskIntoConstraints = false
doThisButton.layer.cornerRadius = 3
doThisButton.layer.backgroundColor = UIColor(hexString: "b7b7b7").CGColor
doThisButton.layer.borderWidth = 0
liftLogStackView.addArrangedSubview(doThisButton)
在 Apple 的文档中,我发现 UILayoutGuide
似乎可行,但现在我不这么认为。我试过这个:
let container = UILayoutGuide()
doThisButton.addLayoutGuide(container)
doThisButton.leadingAnchor.constraintEqualToAnchor(container.leadingAnchor, constant: 8.0).active = true
doThisButton.trailingAnchor.constraintEqualToAnchor(container.trailingAnchor, constant: 8.0).active = true
liftLogStackView.addArrangedSubview(doThisButton)
container.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
并没有什么区别。
很多 SO 搜索都没有找到针对我的问题的任何答案,所以我希望有人能提供帮助。提前致谢。
您需要更改 UIStackView
的对齐方式。默认情况下,它被设置为子视图将填充垂直于轴的维度。在你的情况下,宽度。
liftLogStackView.alignment = .center
这将应用于 UIStackView
中的所有子视图,这可能不是您想要的。在这种情况下,只需将按钮添加为另一个 UIView
中的子视图,然后将该视图添加到 UIStackView
。您的新视图将是全宽的,但您可以将按钮限制为您想要的任何大小。
此外,我建议阅读 UIStackView
Documentation。那里有很多关于如何设置布局的有用信息。
另一个解决方案是将这个 UIButton
打包成一个 UIView
。然后 UIView
将伸展并占据所有需要的位置。 UIButton
将保持您定义的大小。现在您可以根据容器视图定义约束。
我做了一个简短的游乐场示例:
import UIKit
import PlaygroundSupport
class TestViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Test"
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
self.view.backgroundColor = UIColor.brown
let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
stackview.axis = .vertical
stackview.distribution = UIStackViewDistribution.fillEqually
let text = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
text.text = "Hello, i am a label"
text.backgroundColor = UIColor.green
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 100))
containerView.backgroundColor = UIColor.red
containerView.addSubview(text)
stackview.addArrangedSubview(containerView)
let text2 = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
text2.text = "Hello, i am another label"
text2.backgroundColor = UIColor.orange
stackview.addArrangedSubview(text2)
self.view.addSubview(stackview)
}
}
let testController = TestViewController()
PlaygroundPage.current.liveView = testController.view
testController
我有一个简单的垂直 UIStackView
,在第 3 个子视图中,我想放置以编程方式创建的 UIButton
。我可以这样做,但按钮正在扩展到子视图的整个宽度和高度,而不是我在代码中设置的大小。我使用故事板创建了 UIStackView
,但我以编程方式添加此按钮,因此我可以更好地控制按钮的外观。
let doThisButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
doThisButton.setTitle("Let's do this.", forState: .Normal)
doThisButton.setTitleShadowColor(UIColor.blackColor(), forState: .Highlighted)
doThisButton.translatesAutoresizingMaskIntoConstraints = false
doThisButton.layer.cornerRadius = 3
doThisButton.layer.backgroundColor = UIColor(hexString: "b7b7b7").CGColor
doThisButton.layer.borderWidth = 0
liftLogStackView.addArrangedSubview(doThisButton)
在 Apple 的文档中,我发现 UILayoutGuide
似乎可行,但现在我不这么认为。我试过这个:
let container = UILayoutGuide()
doThisButton.addLayoutGuide(container)
doThisButton.leadingAnchor.constraintEqualToAnchor(container.leadingAnchor, constant: 8.0).active = true
doThisButton.trailingAnchor.constraintEqualToAnchor(container.trailingAnchor, constant: 8.0).active = true
liftLogStackView.addArrangedSubview(doThisButton)
container.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
并没有什么区别。
很多 SO 搜索都没有找到针对我的问题的任何答案,所以我希望有人能提供帮助。提前致谢。
您需要更改 UIStackView
的对齐方式。默认情况下,它被设置为子视图将填充垂直于轴的维度。在你的情况下,宽度。
liftLogStackView.alignment = .center
这将应用于 UIStackView
中的所有子视图,这可能不是您想要的。在这种情况下,只需将按钮添加为另一个 UIView
中的子视图,然后将该视图添加到 UIStackView
。您的新视图将是全宽的,但您可以将按钮限制为您想要的任何大小。
此外,我建议阅读 UIStackView
Documentation。那里有很多关于如何设置布局的有用信息。
另一个解决方案是将这个 UIButton
打包成一个 UIView
。然后 UIView
将伸展并占据所有需要的位置。 UIButton
将保持您定义的大小。现在您可以根据容器视图定义约束。
我做了一个简短的游乐场示例:
import UIKit
import PlaygroundSupport
class TestViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Test"
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
self.view.backgroundColor = UIColor.brown
let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
stackview.axis = .vertical
stackview.distribution = UIStackViewDistribution.fillEqually
let text = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
text.text = "Hello, i am a label"
text.backgroundColor = UIColor.green
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 100))
containerView.backgroundColor = UIColor.red
containerView.addSubview(text)
stackview.addArrangedSubview(containerView)
let text2 = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
text2.text = "Hello, i am another label"
text2.backgroundColor = UIColor.orange
stackview.addArrangedSubview(text2)
self.view.addSubview(stackview)
}
}
let testController = TestViewController()
PlaygroundPage.current.liveView = testController.view
testController