简体   繁体   English

将按钮添加到堆栈视图完全占用父堆栈视图

[英]Adding button to stack view entirely takes up of parent stack view

I have been trying to add buttons to a dynamic vertical stack view, the stack view is in a scroll view that's in a horizontal stack view.我一直在尝试将按钮添加到动态垂直堆栈视图,堆栈视图位于水平堆栈视图中的滚动视图中。 Whenever I add a button to the stack view it takes up the entire horizontal stack view, here it is:每当我向堆栈视图添加一个按钮时,它都会占用整个水平堆栈视图,这里是:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let button = UIButton()
        button.setTitle("test", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        Stack.addArrangedSubview(button)
        Stack.translatesAutoresizingMaskIntoConstraints = false
    }

Entire code:完整代码:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var emotionsStack: UIStackView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let button = UIButton()
        button.setTitle("test", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = true
        button.frame.size = CGSize(width: emotionsStack.frame.width, height: (emotionsStack.frame.width/10))
        emotionsStack.addArrangedSubview(button)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches,
                           with: event)
        self.view.endEditing(true)
    }
    }


EDIT:编辑: 解释

What happens is that the stackview takes over the textview (that is in red).发生的情况是 stackview 接管了 textview(即红色)。 This isn't supposed to happen.这不应该发生。

在此处输入图像描述

as stackView didn't detect the size so you have to give each button a fixed size width or height depends on the type of stackView "Vertical or Horizontal" so try to give your button a width and it will work.因为 stackView 没有检测到大小,所以你必须给每个按钮一个固定大小的宽度或高度取决于 stackView “垂直或水平”的类型,所以试着给你的按钮一个宽度,它会起作用。

-> you can use stackView width or height and minus the spaces then divide it by number of buttons and then assign the value for each button -> 您可以使用 stackView 宽度或高度并减去空格,然后将其除以按钮数,然后为每个按钮分配值

Edited: After editing my answer I have added a block of code and an image if you need like this, if not, provide an image which shows which result do you want:编辑:编辑我的答案后,如果您需要这样,我已经添加了一段代码和一张图片,如果没有,请提供一张图片来显示您想要的结果:

import UIKit

class StackViewController: UIViewController {
    
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var emotionsStack: UIStackView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        for i in 0...10 {
        let button = UIButton()
        button.setTitle("test \(i)", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = true
        button.frame.size = CGSize(width: emotionsStack.frame.width, height: ((emotionsStack.frame.width - 30 )/10))
        emotionsStack.addArrangedSubview(button)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches,
                           with: event)
        self.view.endEditing(true)
    }
}

result:结果:

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM