ScrollView disable on Device Orientation

19 Views Asked by At

I am using ScrollView on the screen while in portrait mode. When I change the orientation, can I disable the scrolling? ScrollView can be disabled from the storyboard or it needs to be solved using methods. I have tried size class to manage the disable and enable the scrolling.

1

There are 1 best solutions below

0
DonMag On

These days, the concept of Portrait or Landscape has fallen out of favor. Your app may be running in multi-tasking mode on an iPad, for example, and could be "tall and narrow" even though the device is being held in "landscape" orientation.

It seems a little odd that you'd want to disable scrolling, but assuming that is your design goal -- you can set it in viewDidLayoutSubviews:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    // disable scrolling if view's width is greater than its height
    scrollView.isScrollEnabled = view.frame.height > view.frame.width
}