In UIScrollView
, I have content height and width greater than scrollview's size. So basically it can scroll horizontally and vertically. What I want is to scroll vertically only and I manage horizontally scroll using an UIButton
by using below code.
[scrlViewQuestion setContentOffset:CGPointMake(questionWidth, 0) animated:YES];
Preventing horizontal scrollview, one can just set scrollview content width lesser than scrollview size but in my case scrollview content width is greater than its size? So what is the best way to solve this?
Content UIView width should be equal to the width of UIScrollView's superview for instance, not UIScrollView itself.
SWIFT 5
first, you should create a delegation class or extend the view control with UIScrollViewDelegate
, and after, you should check the content offset with scrollViewDidScroll(_:)
func and disable it with that:
here is a sample code:
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x != 0 {
scrollView.contentOffset.x = 0
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With