UIStackView can I add it with autoresizing instead of autolayout

291 views Asked by At

I've tried to place UIStackView inside UIScrollView with autoresizing masks .flexibleWidth, .flexibleHeight -> i.e. stretch horizontally/vertically to fill scroll view but stack view does not appear if I changed this to autolayout code with leading/trailing/bottom/top constraints then views appear correctly

toolbar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        toolbar.backgroundColor = .clear
        toolbar.axis = .horizontal
        toolbar.distribution = .fill
        toolbar.alignment = .fill
        toolbar.spacing = 8


        toolbarScroll.frame = bounds
        toolbarScroll.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        toolbarScroll.showsHorizontalScrollIndicator = false
        toolbarScroll.showsVerticalScrollIndicator = false
        toolbarScroll.backgroundColor = UIColor.green

        toolbarScroll.addSubview(toolbar)

Here is code that if added after addSubview then toolbar show in toolbarScroll

toolbar.translatesAutoresizingMaskIntoConstraints = false

toolbar.leadingAnchor.constraint(equalTo: toolbarScroll.leadingAnchor).isActive = true
toolbar.trailingAnchor.constraint(equalTo: toolbarScroll.trailingAnchor).isActive = true
 toolbar.bottomAnchor.constraint(equalTo: toolbarScroll.bottomAnchor).isActive = true
 toolbar.topAnchor.constraint(equalTo: toolbarScroll.topAnchor).isActive = true
1

There are 1 answers

0
DonMag On

You added toolbar to toolbarScroll, but you didn't give it a frame.

toolbarScroll.addSubview(toolbar)

// just to make sure...
toolbar.translatesAutoresizingMaskIntoConstraints = true

// give the toolbar stackView a frame
toolbar.frame = toolbarScroll.bounds