Disable vertical scroll in UIScrollView Swift

前端 未结 8 997
鱼传尺愫
鱼传尺愫 2021-02-15 00:47

(Don\'t mark this question as duplicated. I read a lot of questions but I don\'t find the answer to my issue.)

My issue is the following: I have a UIScrollView

相关标签:
8条回答
  • 2021-02-15 00:55

    You can simply do this. Swift 4

    self.scrollView.contentSize.height = 1.0 // disable vertical scroll
    
    0 讨论(0)
  • 2021-02-15 00:59

    just set the contentOffset in scrollViewDidScroll

    if scrollView.contentOffset.y > 0 {
        scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
    }
    
    0 讨论(0)
  • 2021-02-15 00:59

    Swift 5

    scrollView.isScrollEnabled = false
    
    0 讨论(0)
  • 2021-02-15 01:03

    In swift

    self.scrollView.contentSize.width = 1.0 // disable vertical scroll

    0 讨论(0)
  • 2021-02-15 01:04

    Since iOS 11 content insets can get adjusted automatically. Try setting your scrollview's behaviour to not adjust it.

    if #available(iOS 11, *) {
        scrollview.contentInsetAdjustmentBehavior = .never
    }
    
    0 讨论(0)
  • 2021-02-15 01:08

    very charm solution is:

         // also don't forget
         // YourViewController: UIViewController, UIScrollViewDelegate {
    
      @IBOutlet weak var scrollView: UIScrollView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        self.scrollView.delegate = self
      }
    
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
         if scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < 0 {
            scrollView.contentOffset.y = 0
         }
      }
    
    0 讨论(0)
提交回复
热议问题