How to construct a ScrollView using Swift?

给你一囗甜甜゛ 提交于 2019-12-20 10:40:23

问题


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


回答1:


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)
    }

}



回答2:


Your outlet is not connected. From the Swift with objective-C book:

When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces @IBOutlet var name: Type with @IBOutlet weak var name: Type! = nil

If this value was not connected, it would remain as nil and you'd get a runtime error when accessing the value.




回答3:


 @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // setup the scroll view
        self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0);

    }


来源:https://stackoverflow.com/questions/24223768/how-to-construct-a-scrollview-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!