Disable vertical scroll in UIScrollView Swift

前端 未结 8 2140
情深已故
情深已故 2021-02-15 00:37

(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:54

    In swift

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

    0 讨论(0)
  • 2021-02-15 00:56

    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)
  • 2021-02-15 01:00

    Swift 5

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

    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:06

    You can simply do this. Swift 4

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

    just set the contentOffset in scrollViewDidScroll

    if scrollView.contentOffset.y > 0 {
        scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
    }
    
    0 讨论(0)
提交回复
热议问题