skip to Main Content

I’m building a scrollView page and it has height constraint ( for example 1300 )
But there is labels need to expand each other ( line height = 0 )

enter image description here

How can i manage scrollView heightConstraint efficient way ?

2

Answers


  1. In order to make scrollView Self sizing you have to set this constraints which pins it to borders of view and doesn’t sets certain height for it but instead it uses height of a content inside of scrollView.

    scrollView.translatesAutoresizingMaskIntoConstraints = false
            
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    
    Login or Signup to reply.
  2. You can also pin a stack view to the scroll view (or to the layout margins of the scroll view) which will allow the resizing according to the content:

    let stackView = UIStackView()
    scrollView.addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
        stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
        stackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
    ])
    

    Just make sure to provide the intrinsicContentSize for the arranged subviews or at least provide the height for each if you’re using .fill, equalSpacing, or equalCentering for the distribution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search