I have a scrollview that is a set width. I constrained it to be the width of the parent view. When I scroll the scrollview there is a lot of space to the right. I only want it to scroll vertically not horizontally.
Can someone please help?
Here's the code
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor, constant : 0).isActive = true
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant : 0).isActive = true
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant :0).isActive = true
scrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
self.scrollViewContentView.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewContentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
self.scrollViewContentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
self.scrollViewContentView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
self.scrollViewContentView.heightAnchor.constraint(equalToConstant: 1000).isActive = true
CodePudding user response:
Maybe there's an issue with the width. Make sure that the scroll view's width is equal to the width of the safe area and then make the subview's (view inside the scroll view) width equal to that of scroll view.
Also check if the leading, trailing, top and bottom constraints are set as 0 and not -1 or -2.