简体   繁体   中英

Scroll View ios 8 Xcode not Scrolling - Swift

I am very new to Swift and developing on ios but I cannot find a way to make the UIScrollView to scroll down and stay down. I have been trying tons of tutorials over this and still nothing. I have a ContentView element inside of my ScrollView element. This ContentView has all of my boxes that I want it to scroll through but it does not scroll. It does though bounce if that makes any difference...can anyone send me in the right direction?

Try setting the contentSize in viewDidLayoutSubviews

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

I finally figured it out after 2 days and it was very simple. All I did was set the scrollView to the size of the screen but then set the content view height at 1500px and it started working. Thanks everyone!

Sounds like uou may not have set the contentSize for your scroll view. To do that if you're not using Auto layout:

scrollView.contentSize = // The size of your content.

If you are using Auto layout you need to make sure you have NSLayoutConstraints from each edge of your content to each corresponding edge of your UIScrollView . By doing this the content size of your UIScrollView will be set automatically.

Hope that helps.

Just wanted to add a note to FreeTheStudentsAnswer in case anyone had a similar issue to me - I set the scrollView to the size of the screen and then set the content view height at a larger value and it started working only when I set these in the viewDidAppear instead of viewDidLoad . Not sure if that will help anyone, but that fixed my issue

In scrollview, scroll happens only if the scrollview has enough space to scroll its content.

You said the scrollview and its content view have the same dimensions. Then it won't scroll. Scrollview.contentSize should be at least double the size of scrollview if you want it to scroll.

Hint: scrollview dimension should not be greater than the iPhone screen size. And scrollview content size should be greater than the scrollview. Scroll view scrolls through its contentview.

SWIFT 4.0 Update

@IBOutlet weak var myScrollView: UIScrollView!

  override func viewDidLoad() {
    super.viewDidLoad()

    myScrollView.alwaysBounceVertical = true
    myScrollView.alwaysBounceHorizontal = true
    myScrollView.isScrollEnabled = true
    myScrollView.contentSize = CGSize(width: 375, height: 1000)
}

Try This

1.scrollView.bounds = YES;
2.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: YourContentView.frame.size.height);

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