Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One of the subviews of a stack view not showing up at all

I want my stack view to have three views: red image, blue image and register view. The thing is that while the red and blue button appear just fine, the register view does not.

enter image description here

This is how I set up and place my stack view inside my view controller view:

func setupSocialStackView() {
    let socialStackView = UIStackView(arrangedSubviews: redImage, blueImage, registerView])
    socialStackView.axis = .vertical
    socialStackView.distribution = .fillProportionally
    view.addSubview(socialStackView)
    socialStackView.spacing = 8

    // NSLayoutAnchor constraints here to place stack view 
    // inside my view controller view
}

This is the code for my register view which doesn't show up:

lazy var registerView: UIView = {
    let containerView = UIView()

    // Register button
    let registerButton = UIButton(type: .system)
    registerButton.setTitle("Register", for: .normal)
    registerButton.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightLight)
    registerButton.titleLabel?.textAlignment = .center
    registerButton.setTitleColor(UIColor.black, for: .normal)
    registerButton.titleLabel?.textColor = UIColor(r: 91, g: 90, b: 90)
    registerButton.addTarget(self, action: #selector(presentRegisterController), for: .touchUpInside)
    containerView.addSubview(registerButton)

    return containerView
}()

The other two arrange views of the stack view are UIImageViews.

Why are the two images there and the register view is not? Am I missing something?

like image 680
Cesare Avatar asked Sep 12 '25 06:09

Cesare


2 Answers

You don't appear to be giving your UIButton a frame size. See what you get if you add an autoresizingMask line:

    registerButton.addTarget(self, action: #selector(presentRegisterController), for: .touchUpInside)

    registerButton.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    containerView.addSubview(registerButton)
like image 188
DonMag Avatar answered Sep 14 '25 20:09

DonMag


For me, I needed to add bottom, right, and left constraints for the view inside the stack view. Then it appeared.

like image 33
fullmoon Avatar answered Sep 14 '25 21:09

fullmoon