0

I am building an app using a codeschool tutorial, but even though I have followed the exact code and steps to build a scrollView page correctly, it only displays a blank screen on the iOS simulator. What am I doing incorrectly?

Here is my code:

import UIKit

class ContactViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(scrollView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        scrollView.contentSize = CGSize(width: 375, height: 800)

I get the following error message:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0) when building the app, which is summarized as that states

...which is characterized as a:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I have run this numerous times. And I have looked carefully through existing posts, but I am a novice and some are suggesting changes to the code that seem like they would be unnecessary, since I've done everything exactly as the tutorial instructs (which I imagine would have had the appropriate QA in advance). I appreciate anyone's insight!

UPDATE. Users below have helped identify the problem. The code itself was not the problem, but simply linking the scrollView to the code in the Assistant Editor had apparently not been done. Even though there was a circle next to the code in the Assistant Editor, it was not actually linked, or the link had been broken. You can confirm the link is actually working by hovering over the circle, which will highlight the Scroll View in your storyboard editor in blue. If the link is broken, it will not highlight.

Thanks for everyone's help. And here again is the final working code:

Pigpocket
  • 449
  • 5
  • 24
  • At which location you are getting this error? – Ashish Kakkad Nov 21 '16 at 04:30
  • 1
    I think the problem is with scrollView. Did you connect your scrollview properly in xib or storyboard file? – Venk Nov 21 '16 at 04:37
  • @AshishKakkad it says that the view.addSubview(scrollView) line has the error. – Pigpocket Nov 21 '16 at 05:10
  • @Hogotron Have you verified your scroll view is actually connected to the outlet? Your error indicates that you have not done so. Please check. – rmaddy Nov 21 '16 at 05:49
  • @Hogotron you first comment this line self.view.addSubview(scrollView) and check your scrollview's outlet in storyboard connected or not – Raj Joshi Nov 21 '16 at 06:02
  • rmaddy and @RajJoshi you are both, in fact, correct. My scroll view was not actually connected, even though there was a circle next to the code indicating as much. I have updated my comment to reflect that. Thank you! – Pigpocket Nov 21 '16 at 06:17

2 Answers2

0

You have done everything right. Add UIScrollViewDelegate after UIViewController separated by a comma. Then Do these steps 1)Select the scrollView and in the attribute inspector tick scrolling enabled and all the bounces. 2)Set the constraints properly (this is the main thing) 3)set this code on your viewDidLoad

scrollView.isScrollEnabled = true; 
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0)
danutha
  • 214
  • 3
  • 16
  • Hi danutha. thanks for responding! I tried this and seem to be still receiving the error. Please take a look: import UIKit class ContactViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() view.addSubview(scrollView) } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() scrollView.contentSize = CGSize(width: 375, height: 800) – Pigpocket Nov 21 '16 at 05:07
  • Hi tried your code and fixed the issue. Check the following things first. 1) Select the scrollView and in the attribute inspector tick scrolling enabled and all the bounces. 2) Set the constraints properly (this is the main thing) – danutha Nov 21 '16 at 05:41
  • 3) set this code on your viewDidLoad scrollView.isScrollEnabled = true; self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0) – danutha Nov 21 '16 at 05:43
  • Hey danutha, check out my updated edit above. I'm not sure if I made your suggested changes correctly. Please let me know. I am still getting athe same error, however, now it is on the line scrollView.isScrollEnabled = true line. – Pigpocket Nov 21 '16 at 05:54
  • Still its not updated it seems anyways, did u delete the function viewWillLayoutSubviews and the code line self.view.addSubview(scrollView) ?? did u add the UIScrollViewdelegate ? – danutha Nov 21 '16 at 05:59
0

Your are going right, Your scrollview is not seeing due to scrollview background colour since it is clear so you add one more line your code

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    //Comment this line
  //  self.view.addSubview(scrollView)
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    scrollView.backgroundColor = UIColor.cyan
    scrollView.contentSize = CGSize(width: 375, height: 800)
}
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37