0

我尝试将三个子视图添加到堆栈视图。但是我添加的只有最后一个可见。我尝试使用构造函数添加一些子视图,但也使用addArangedSubview(). 两种方式我得到相同的结果。看起来要么视图彼此重叠,要么被下一个添加的视图替换。我该如何解决这个问题并添加几个可见的子视图?

我有一个视图,我在其中创建堆栈视图和子视图,以及一个视图控制器,我在其中添加此视图。

看法:

import UIKit

class WhatToCookView: UIView {

    var originalMealButton: CustomButtonXLView!
    var predefinedMealButton: CustomButtonXLView!
    var ourSuggestionsButton: CustomButtonXLView!
    var stackView: UIStackView!

    init() {
        super.init(frame:CGRect.zero)
        self.backgroundColor = .gray

        let transpatent = UIColor.blue.cgColor

        self.originalMealButton = CustomButtonXLView(bgColorStart: transpatent, bgColorEnd: transpatent, cornerRadius: 6, mainText: "Button1", subText: "Subtext", icon: UIImage(named: "cooker_2")!)
        self.predefinedMealButton = CustomButtonXLView(bgColorStart: transpatent, bgColorEnd: transpatent, cornerRadius: 6, mainText: "Button2", subText: "Subtext", icon: UIImage(named: "cooker_2")!)
        self.ourSuggestionsButton = CustomButtonXLView(bgColorStart: transpatent, bgColorEnd: transpatent, cornerRadius: 6, mainText: "Button3", subText: "Subtext", icon: UIImage(named: "cooker_2")!)

        self.stackView = UIStackView(arrangedSubviews: [self.originalMealButton, self.ourSuggestionsButton, self.predefinedMealButton])
        self.stackView.axis = .vertical
        self.stackView.distribution = .fillEqually
        self.stackView.alignment = .fill
        self.stackView.spacing = 10
        self.stackView.backgroundColor = .green

        self.addSubview(self.stackView)
        self.setupButtonConstraints()

       // self.stackView.addArrangedSubview(self.originalMealButton)
       // self.stackView.addArrangedSubview(self.predefinedMealButton)
       // self.stackView.addArrangedSubview(self.ourSuggestionsButton)

        return
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupButtonConstraints() {

        self.stackView.translatesAutoresizingMaskIntoConstraints = false
        self.stackView.heightAnchor.constraint(equalTo:self.heightAnchor).isActive = true
        self.stackView.widthAnchor.constraint(equalTo:self.widthAnchor).isActive = true

        self.originalMealButton.translatesAutoresizingMaskIntoConstraints = false
        self.predefinedMealButton.translatesAutoresizingMaskIntoConstraints = false
        self.ourSuggestionsButton.translatesAutoresizingMaskIntoConstraints = false

    }

视图控制器:

import UIKit

class WhatToCookViewController: UIViewController {

    var whatToCookView: WhatToCookView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.whatToCookView = WhatToCookView()
        view.addSubview(self.whatToCookView)

        self.setupConstraints()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func setupConstraints() {
        self.whatToCookView.translatesAutoresizingMaskIntoConstraints = false
        self.whatToCookView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        self.whatToCookView.heightAnchor.constraint(equalTo:view.heightAnchor, multiplier:0.5).isActive = true
    }

}

此外,如果我尝试不使用构造函数添加排列子视图,而是使用 addArrangedSubview(),则始终只有最后添加的子视图可见。结果是: 在此处输入图像描述

4

0 回答 0