简体   繁体   English

我如何向放置在UIStackView中的以编程方式创建的UIButton添加约束

[英]How do I add constraints to a programmatically created UIButton placed in a UIStackView

I have a simple vertical UIStackView and in the 3rd subview, I want to place my programmatically created UIButton . 我有一个简单的垂直UIStackView ,在第三UIStackView视图中,我想放置以编程方式创建的UIButton I can do this but the button is expanding to the full width and height of the subview instead of being the size I'm setting it to in my code. 我可以这样做,但是按钮会扩展到子视图的整个宽度和高度,而不是我在代码中设置的大小。 I've created the UIStackView using the storyboard but I'm adding this button programmatically so I have better control over the look of the button. 我已经使用情节UIStackView创建了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)

In Apple's documentation, I found UILayoutGuide which seemed like it might work but now I don't think so. 在Apple的文档中,我找到了UILayoutGuide ,它似乎可能UILayoutGuide ,但现在我不这么认为。 I tried this: 我尝试了这个:

  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

and it made no difference. 没什么区别。

A lot of SO searching didn't turn up any answers specific to my problem, so I'm hoping someone can help. 许多SO搜索并没有针对我的问题提供任何答案,因此我希望有人可以提供帮助。 Thanks in advance. 提前致谢。

You need to change the alignment of the UIStackView . 您需要更改UIStackView的对齐方式。 By default it is set so subviews will fill the dimension perpendicular to the axis. 默认情况下已设置,因此子视图将填充垂直于轴的尺寸。 In your case, the width. 在你的情况下,宽度。

liftLogStackView.alignment = .center

That will apply to all subview in the UIStackView which may not be what you want. 这将应用于UIStackView所有子视图,而这可能并不是您想要的。 In that case, just add the button as a subview in another UIView then add that view to the UIStackView . 在这种情况下,只需将按钮添加为另一个UIView的子视图,然后将该视图添加到UIStackView Your new view will then be full width but you can constrain the button to whatever size you want. 这样,您的新视图将变为全角,但是您可以将按钮限制为所需的任何大小。

Also, I'd suggest reading the UIStackView Documentation . 另外,我建议阅读UIStackView文档 There is a lot of useful information in there about how to setup your layouts. 那里有很多关于如何设置布局的有用信息。

Another solution would be to pack this UIButton into a UIView . 另一个解决方案是将该UIButton打包到UIView Then the UIView will stretch out and take all place needed. 然后, UIView将伸展并占据所有需要的位置。 The UIButton will stay the size you have defined. UIButton将保持您定义的大小。 Now you are able to define the constraints depending on the container view. 现在,您可以根据容器视图定义约束。

I have made a short Playground example: 我做了一个简短的Playground示例:

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

故事板示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式在UITableViewCell swift中为UIStackView添加约束? - How to add constraints to UIStackView inside of a UITableViewCell swift programmatically? 如何以编程方式向UISegmentedControl添加约束 - How do I add constraints programmatically to UISegmentedControl 如何以编程方式将约束添加到以编程方式创建的UIView? - How to add constraints programmatically to a programmatically created UIView? 如果使用VFL以编程方式创建约束,如何为约束变化设置动画? - How do I animate constraint changes if constraints are created programmatically with VFL? 如何以编程方式修改UIButton的约束 - How to modify programmatically constraints of UIButton 如何以编程方式将笔尖添加到UIStackView - How to add nibs to a UIStackView programmatically 如何在UITableViewCell中的UIStackView中添加UIView? - How do I add a UIView to a UIStackView in a UITableViewCell? 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? - How to add UIViewController as target of UIButton action created in programmatically created UIView? 有没有办法访问UIStackView放置的约束优先级? - Is there a way to access the priority of constraints placed by UIStackView? 当我以编程方式将其添加到UIstackview时,UIView不会隐藏 - UIView is not hiding when I add it programmatically to UIstackview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM