簡體   English   中英

我如何向放置在UIStackView中的以編程方式創建的UIButton添加約束

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

我有一個簡單的垂直UIStackView ,在第三UIStackView視圖中,我想放置以編程方式創建的UIButton 我可以這樣做,但是按鈕會擴展到子視圖的整個寬度和高度,而不是我在代碼中設置的大小。 我已經使用情節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)

在Apple的文檔中,我找到了UILayoutGuide ,它似乎可能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文檔 那里有很多關於如何設置布局的有用信息。

另一個解決方案是將該UIButton打包到UIView 然后, UIView將伸展並占據所有需要的位置。 UIButton將保持您定義的大小。 現在,您可以根據容器視圖定義約束。

我做了一個簡短的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM