0

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: enter image description here

Ryan
  • 4,799
  • 1
  • 29
  • 56
manban
  • 133
  • 1
  • 19
  • 1
    You have constrained the stack views width and height, but you haven't constrained any of its edges, so auto layout doesn't know where to put it. You either need to constrain all four edges *or* the width/height and either top or bottom and left or right; – Paulw11 Oct 11 '17 at 19:50
  • Is that your actual code? I just ran it as a test... the ONLY thing I changed was replacing your `CustomButtonXLView` with standard `UIButton`, and this is the result: https://i.imgur.com/bpi4lqs.png – DonMag Oct 11 '17 at 20:13
  • Note: even though my test *looks* correct, Debug View Hierarchy shows that the Position of `whatToCookView` is ambiguous... so you also want to add the additional constraints as mentioned by @Paulw11 – DonMag Oct 11 '17 at 20:38
  • 1
    Thanks for the help. The problem were the missing edge constraints. I added topAnchor and leadingAnchor for the stackview. – manban Oct 12 '17 at 20:47

0 Answers0