How to disable horizontal scrolling of UIScrollView?

前端 未结 14 1943
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 08:17

I have a UIView like iPhone\'s Springboard. I have created it using a UIScrollView and UIButtons. I want to disable horizontal scrolli

14条回答
  •  一向
    一向 (楼主)
    2020-12-02 08:51

    Introduced in iOS 11 is a new property on UIScrollView

    var contentLayoutGuide: UILayoutGuide
    

    The documentation states that you:

    Use this layout guide when you want to create Auto Layout constraints related to the content area of a scroll view.

    Along with any other Autolayout constraints that you might be adding you will want to constrain the widthAnchor of the UIScrollView's contentLayoutGuide to be the same size as the "frame". You can use the frameLayoutGuide (also introduced in iOS 11) or any external width (such as your superView's.)

    example:

    NSLayoutConstraint.activate([
      scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: self.widthAnchor)
    ])
    

    Documentation: https://developer.apple.com/documentation/uikit/uiscrollview/2865870-contentlayoutguide

提交回复
热议问题