简体   繁体   中英

UIScrollView not scrolling in Swift

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!

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

}

override func viewDidLayoutSubviews() {
    scroller.scrollEnabled = true
    // Do any additional setup after loading the view
    scroller.contentSize = CGSizeMake(400, 2300)
}

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 the following to your viewDidLoad method:

scroller.contentSize = CGSize(width: 400, height: 2300)

Alot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners. Ciao :-)

@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
// Do any additional setup after the view
}

override func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 800)
}

This code will work perfectly fine as long as you do what I said up above

If you are using AutoLayout

Set content size in viewDidAppear which works for me.

override func viewDidAppear(_ animated: Bool) {
   scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+300)
}

斯威夫特 3.0 版本

scroller.contentSize = CGSize(width: scroller.contentSize.width, height: 2000)

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.

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.

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 .

The problem could be that your scrollView doesn't know its contentSize like stated above, but the fix is easier than what the above answers are. Like Carlos said but I will elaborate more. If you want your scrollView to scroll vertically(up & down), make your contentView which is in the hierarchy of the scrollView equal width to the ViewController and give it a height constraint that works for your project ie 700. For the opposite(horizontally) make the height equal to the ViewController and the width some big number that works for your project.

FWIW, I found that I needed to use sathish's solution from above, though it was insufficient to effect the intervention in viewDidAppear alone. I had to instead make the adjustment for every new content assignment:

func display(pattern: Pattern) {
    let text : NSAttributedString = pattern.body()
    patternTextView.attributedText = text

    // Set the size of the view. Autolayout seems to get in the way of
    // a correct size calculation
    patternTextView.contentSize = CGSize(width: 348, height: 620)
}

The manifest constants (yeah, I know, yuk, but it makes it easier to understand here) are from the autolayout spec.

It worked for me. In Size Inspector Layout = Translates Mask into constraints. Autoresizing = all click.

在此处输入图片说明

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

The view might not seem to move down for two reasons as your code is perfectly sound.

  • First reason, you don't see the scroll indicator
  • Second reason, theres nothing in the view to display

The second reason is not enough to say your view isn't scrolling but if you do not see the scroll indicator, then you might believe the view does not scroll when it in fact does.

The first reason I see happening when the constraints are not set in the storyboard especially if the view controller size in the storyboard is set to "Any". The scroll view would be wider then the screen of the simulated or real device and thus the scroll indicator would be outside of the visible screen.

I've had problems with this, and couldn't find the answer for me until I worked it out. I had a scroll view inside of a normal view and as soon as I replaced the 'view' with a 'scroll view' it scrolled fine. Just incase anyone stumbles on that.

I'm not sure if thats meant to stop it from working as I'm very inexperienced but it seemed to be my solution. Hope it helps someone.

For Swift 5.6 and iOS 15:

let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
let subView: UIView = UILabel()
subView.text = String(repeating: "MMMMMMM ", count: 100)
NSLayoutConstraint.activate([
    subView.topAnchor.constraint(equalTo: scrollView.topAnchor),
    subView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
    subView.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
    subView.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
    // Constrain width so the label text wraps and we scroll vertically.
    subView.widthAnchor.constraint(lessThanOrEqualTo: scrollView.widthAnchor),
])

Increase the content Height work for me.

I do not know it is a good solution, but you can try to set headerview to empty UITableView.

let scrollView: UIView = UIView(frame: CGRectMake(0, 0, 400, 2300))

tableView.tableHeaderView = scrollView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM