Home > other >  I can't set the BackgroundColor of a StackView in iOS 12 or older
I can't set the BackgroundColor of a StackView in iOS 12 or older

Time:04-01

I'm trying to set a backgroundColor of a StackView and it works on iOS 13 or newer, but on iOS 12 it's a complete failure. Maybe it's related to darkMode although I don't use any related settings.

I tried to set the color in VidDidLoad() and it didn't work either, I'm kind of lost, I looked for other similar questions, none were the same

ViewCode

final class PaymentCashViewCode: UIView, ViewCodeProtocol {
var salesValueComponent: UIStackView = {
    let view = UIStackView()
    view.backgroundColor = UIColor(named: .greyRhino)
    view.isLayoutMarginsRelativeArrangement = true
    view.axis = .vertical
    return view
}() 
}

ViewController

class PaymentCashViewController: UIViewController {
var mainView = PaymentCashViewCode()

func setupComponents() {
    self.view.addAttrachedSubView(view: mainView)
    self.title = viewModel.getViewControllerTitle()
    mainView.delegate = self
    mainView.setTextFieldDelegate(delegate: self)
    mainView.setTextFieldFocus()
}
}

CodePudding user response:

Prior to iOS 14 stack views were "non-rendering" views -- that is, they only arranged their subviews.

Starting with iOS 14 you can set the background color.

If you need a background color for earlier versions, you need to embed the stack view in, or place it on top of, a UIView

  • Related