Need Help in applying constraints to UIScrollView

2019-09-20 17:35发布

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

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

3条回答
倾城 Initia
2楼-- · 2019-09-20 18:03

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() {

    }
查看更多
Anthone
3楼-- · 2019-09-20 18:04

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
查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-20 18:05

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

查看更多
登录 后发表回答