Need Help in applying constraints to UIScrollView

2019-09-20 17:28发布

问题:

I'm adding subviews programmatically. Requirement : when extra views are added the scroll should be enabled. The constraints that I applied :

To remove extra spaces at the top I wrote self.automaticallyAdjustsScrollViewInsets = false also give content size as self.scrollView.contentSize = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height*2)

But the when the view is loaded scroll is completely disabled.

Please help me

回答1:

When you using auto layout, you have to set content size at viewcontroller method, you have to override following method.

-(void)viewWillLayoutSubviews;

swift

override func viewWillLayoutSubviews() {

    }


回答2:

Try to set contentview height, based on (origin.y + height) of subview at bottom in scroll contentview.



回答3:

There are few things that you need to do, check that you have this enabled.

  1. Bounces
  2. Bounces Vertically
  3. Scrolling Enabled

If enabling those still doesn't help with the scrolling, add a Height constraint of your ContentView. You have to connect this constraint as an IBOutlet to your View Controller.

I believe your data is fetched from server, as such you have an ambiguous content height. So at where you print the contentSize of value (375.0,1334.0) in your question, add this line of code and you should be able to scroll thereafter.

YourHeightConstraint.constant = self.scrollView.contentSize.height