Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a ScrollView using Swift?

I'm constructing my first App for IOS and I'm struggling to find a way to do a simple ScrollView using the Swift code on the XCode6, please can someone help me to find the solution?

My problem is that I don't know how to make the scrollview work in my code. I already putted the code as you can see below in the ViewController.swift and I was expecting to be able to select the Outlet "scroller" in the Main.storyboard for a ViewController, instead of this I'm receiving the error *"fatal error: Can't unwrap Optional.None (lldb)"* EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

I have some ViewController screens and in one of that I putted one ScrollView and I want to make it works using the Swift.

I'm stuck on this:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var scroller:UIScrollView

    override func viewDidLoad() {
        super.viewDidLoad()
        scroller.scrollEnabled = true;
        scroller.contentSize = CGSizeMake(320, 624);
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

I think if someone can provide a simple example how to do a scrollview using swift it will solve my problem. Any help is appreciate.

Trying to do it in a old style I tried to do it using a .m and .h file:

ViewController.m

#import "Amigo-Bridging-Header.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 624)];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Amigo-Bridging-Header.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIScrollView *scroller;
}
@end

Cheers

like image 543
Alex Avatar asked Jun 14 '14 19:06

Alex


1 Answers

Let's give this a shot. The one thing to note is I have yet to find a way to downcast self.view as a UIScrollView, so you can't make calls like self.view.contentOffset.

import UIKit

class ScrollingViewController : UIViewController {
    // Create a scrollView property that we'll set as our view in -loadView
    let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)

    override func loadView() {
        // calling self.view later on will return a UIView!, but we can simply call 
        // self.scrollView to adjust properties of the scroll view:
        self.view = self.scrollView

        // setup the scroll view
        self.scrollView.contentSize = CGSize(width:1234, height: 5678)
        // etc...
    }

    func example() {
        let sampleSubView = UIView()
        self.view.addSubview(sampleSubView) // adds to the scroll view

        // cannot do this:
        // self.view.contentOffset = CGPoint(x: 10, y: 20)
        // so instead we do this:
        self.scrollView.contentOffset = CGPoint(x: 10, y: 20)
    }

}
like image 69
Ryan Avatar answered Oct 11 '22 14:10

Ryan