0

I have a view inside a scrollview whose content changes, so its size has to change too and if necessary, the scrollview has to be able to scroll.

This is my Storyboard

enter image description here

At the beginning, the Lv Height Constraint says that Lv should be as high as its superview.

Now, every time the content changes I execute this code (I imported the constraint as an outlet):

    //Content is updated...
    //Determines wether scrolling is enabled by comparing the size of the content and the size of the super view
    var heightMulti: CGFloat
    if(superviewHeight!.isLess(than: svYPos)) {
        heightMulti = svYPos/superviewHeight! //svYPos is the height of the content
        scrollView.isScrollEnabled = true
    } else {
        heightMulti = 1
        scrollView.isScrollEnabled = false
    }
    //Sets height of content view
    lvHeightConstraint.constant = heightMulti
    //Scrolls to the top of the view again
    scrollView.scrollToTop()

But somehow, the height doesn't change, so I can not scroll.

This code does work in 99% of the cases, but I am just adding constraints, which is a bad approach as far as I know

    //Determines wether scrolling is enabled by comparing the size of the content and the size of the super view
    var heightMulti: CGFloat
    if(superviewHeight!.isLess(than: svYPos)) {
        heightMulti = svYPos/superviewHeight!
        scrollView.isScrollEnabled = true
    } else {
        heightMulti = 1
        scrollView.isScrollEnabled = false
    }
    //Sets height of content view
    lv.heightAnchor.constraint(equalTo: superview.heightAnchor, multiplier: heightMulti).isActive = true
    //Scrolls to the top of the view again
    scrollView.scrollToTop()
Patrick
  • 552
  • 5
  • 17
  • This is not how scroll view is supposed to scroll. Hope this answer can help you - https://stackoverflow.com/a/59442100/7271020. You don't need to change the height dynamically or tell scrollView where it should scroll or not. It does it automatically based on child views if the constraints are set correctly. – sandpat Jan 25 '20 at 11:51
  • scrollView does depend on its contentSize – Sanad Barjawi Jan 25 '20 at 13:10
  • @Josh ok, thanks! I changed my code, but somehow my constraints do not work and I dont find the problem. I posted another question, if you could take another look, it would be great! https://stackoverflow.com/questions/59917652/swift-programmatically-added-constraints-dont-work – Patrick Jan 26 '20 at 11:00
  • Lv should have constraints relative to its superview I.e scrollview and not related to view’s bounds for scrolling to happen. – sandpat Jan 26 '20 at 15:56
  • @Patrick - take a look at this answer and see if it would work for you: https://stackoverflow.com/a/59828434/6257435 – DonMag Jan 26 '20 at 16:16
  • @DonMag Thanks, that's working for me! – Patrick Jan 26 '20 at 17:32
  • @DonMag Yes, that's working for me. But didn't you already comment the same link? I edited the question I linked earlier. Now my problem is to specify the leading and trailing constraints of the subviews without warnings (https://stackoverflow.com/questions/59917652/swift-programmatically-added-constraints-dont-work) – Patrick Jan 27 '20 at 13:18
  • @Patrick - yes, I commented the link... but since *that* post solved your question, it makes sense to close this one (for the benefit of other folks who may come across it). I will take a look at your other post. – DonMag Jan 27 '20 at 13:22
  • @Patrick - you accepted an answer at that question... do you still need help with it? – DonMag Jan 27 '20 at 13:24
  • @DonMag Yes, for the edited part, I dont have a solution yet. – Patrick Jan 27 '20 at 13:25

0 Answers0