Sunday, June 26, 2022
HomeiOS Developmentios - Variable-width StackView? - Stack Overflow

ios – Variable-width StackView? – Stack Overflow


That is my code:

import UIKit

class DocumentViewController: UIViewController, UIScrollViewDelegate {
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return scrollViewContainer
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let scrollViewSize = scrollView.bounds.dimension
        let scrollViewContainerSize = scrollViewContainer.body.dimension
        let verticalPadding = scrollViewContainerSize.top < scrollViewSize.top ? (scrollViewSize.top - scrollViewContainerSize.top) / 2 : 0
        let horizontalPadding = scrollViewContainerSize.width < scrollViewSize.width ? (scrollViewSize.width - scrollViewContainerSize.width) / 2 : 0
        scrollView.contentInset = UIEdgeInsets(prime: verticalPadding, left: horizontalPadding, backside: verticalPadding, proper: horizontalPadding)
    }
  
    let scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.minimumZoomScale = 0.1
        scrollView.maximumZoomScale = 4.0
        scrollView.zoomScale = 1.0
        return scrollView
    }()
  
    let scrollViewContainer: UIStackView = {
        let view = UIStackView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.axis = .vertical
        view.spacing = 15
        return view
    }()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        scrollView.delegate = self
        view.backgroundColor = .grey
        view.addSubview(scrollView)
        scrollView.addSubview(scrollViewContainer)
        
        let uiview1 = UIView(body: .zero)
        uiview1.heightAnchor.constraint(equalToConstant: 1000).isActive = true
        uiview1.widthAnchor.constraint(equalToConstant: 1000).isActive = true
        scrollViewContainer.addArrangedSubview(uiview1)
        uiview1.backgroundColor = .white
        
        let uiview2 = UIView(body: .zero)
        uiview2.heightAnchor.constraint(equalToConstant: 1000).isActive = true
        uiview2.widthAnchor.constraint(equalToConstant: 1000).isActive = true
        scrollViewContainer.addArrangedSubview(uiview2)
        uiview2.backgroundColor = .white
        
        let uiview3 = UIView(body: .zero)
        uiview3.heightAnchor.constraint(equalToConstant: 1000).isActive = true
        uiview3.widthAnchor.constraint(equalToConstant: 1000).isActive = true
        scrollViewContainer.addArrangedSubview(uiview3)
        uiview3.backgroundColor = .white
        
        let uiview4 = UIView(body: .zero)
        uiview4.heightAnchor.constraint(equalToConstant: 1000).isActive = true
        uiview4.widthAnchor.constraint(equalToConstant: 1000).isActive = true
        scrollViewContainer.addArrangedSubview(uiview4)
        uiview4.backgroundColor = .white
        
        let uiview5 = UIView(body: .zero)
        uiview5.heightAnchor.constraint(equalToConstant: 1000).isActive = true
        uiview5.widthAnchor.constraint(equalToConstant: 2000).isActive = true
        scrollViewContainer.addArrangedSubview(uiview5)
        uiview5.backgroundColor = .white
        
        showBounds()
        setConstraints()
    }
    
    func setConstraints() {
        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        scrollViewContainer.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
        scrollViewContainer.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
        scrollViewContainer.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        scrollViewContainer.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
        
    }
        
    func showBounds() {
        view.layer.borderWidth = 5.0
        view.layer.borderColor = UIColor.blue.cgColor
        scrollViewContainer.layer.borderWidth = 5.0
        scrollViewContainer.layer.borderColor = UIColor.crimson.cgColor
        scrollView.layer.borderWidth = 3.0
        scrollView.layer.borderColor = UIColor.yellow.cgColor
    }

}

This code offers the next behaviour:
https://i.stack.imgur.com/gNxnD.gif

You’ll be able to see that whereas the final take a look at UIView I made in viewDidLoad has a width of 2000, it finally ends up being lower off for some motive, as if its width is 1000. Moreover, if I modify the width of the final view to 500, I get this behaviour.

So it appears that evidently scrollViewContainer (?) is resizing to the smallest width out of these take a look at UIViews. All I need is for it to permit totally different sized views, with the default width being the widest little one view, as in this mockup.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments