Stackview添加多个子视图

时间:2017-10-11 19:43:03

标签: ios swift stackview

我尝试将三个子视图添加到stackview。但只有最后一个我添加可见。我尝试使用构造函数添加一些子视图,但也使用addArangedSubview()。两种方式我得到相同的结果。 它看起来要么是视图相互叠加,要么替换为下一个视图。我怎么能解决这个问题并添加几个可见的子视图?

我有一个视图,我在其中创建了stackview和子视图以及我添加此视图的viewcontroller。

查看:

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

    }

的ViewController:

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
    }

}

此外,如果我尝试使用构造函数添加arrangeSubviews,但使用addArrangedSubview(),则始终只有最后添加的子视图可见。结果是: enter image description here

0 个答案:

没有答案