I try to add three subviews to a stackview. But there is only the last that I add visible. I tried to add some subviews with the constructor, but also with addArangedSubview()
. Both ways I get the same result.
It looks like either the views are on top of each other or replaced by the next added one. How could I solve this and add several visible subview?
I have a view, where I create the stackview and subviews and a viewcontroller where I add this view.
View:
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
}
}
Also if I try to add arrangeSubviews not with the constructor, but with addArrangedSubview(), there is always only the last added subview visible. The result is: