UIScrollView not scrolling in Swift

前端 未结 12 1054
Happy的楠姐
Happy的楠姐 2021-02-03 19:03

My UIScrollView won\'t scroll down. I don\'t know why. I already followed Apple documentation regarding to this issue.

@IBOutlet weak var scroller: UIScrollView!         


        
相关标签:
12条回答
  • 2021-02-03 19:29

    If you are using Storyboard:

    1. Put your Content view inside the UIScrollView
    2. Add top, bottom, left and right constraints with the scroll view
    3. Add equal heights and widths constraints
    4. For a vertical scroll set the Equal Heights Constraint priority to 250. For a horizontal scroll set the Equal Widths Constraint priority to 250
    0 讨论(0)
  • 2021-02-03 19:31

    You need to set the frame of your UIScrollView so that it is less than the contentsize. Otherwise, it won't scroll.

    Also, I would recommend that you add scroller.contentSize = CGSizeMake(400, 2300) to your viewDidLoad method.

    0 讨论(0)
  • 2021-02-03 19:31

    Swift 3.0 version

    scroller.contentSize = CGSize(width: scroller.contentSize.width, height: 2000)
    
    0 讨论(0)
  • 2021-02-03 19:31

    In my case, I used UIStackView inside UIScrollView.

    Added some views-elements from code to stackview. It won't scroll.

    Fixed it by setting stackview's userInteractionEnabled to false.

    0 讨论(0)
  • 2021-02-03 19:32

    Do not give fix height to scroll view and always give top of first subview to scrollview and bottom of last subview to scrollview. By this way scroll view will automatically grow as per the size of contained subviews. No need to give contentSize to the scrollview.It will work for small as well as large size iPhone.

    0 讨论(0)
  • 2021-02-03 19:33

    If you are using autolayout, then the contentSize property stops working and it will try to infer the content size from the constraints. If that is the case, then your problem could be that you are not defining the necessary constraints to the content view so that the scrollview can infer the content size. You should define the constraints of your content view to the top and bottom edges of the scrollview.

    0 讨论(0)
提交回复
热议问题