如何使用Swift构建ScrollView? [英] How to construct a ScrollView using Swift?

查看:88
本文介绍了如何使用Swift构建ScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个IOS应用程序,我很难找到一种方法来使用XCode6上的Swift代码来做一个简单的ScrollView,有人可以帮我找到解决方案吗?



我的问题是我不知道如何在我的代码中使scrollview工作。我已经在ViewController.swift中看到了下面的代码,我希望能够在Main.storyboard中为ViewController选择Outletscroller,而不是我收到错误 * 致命错误:无法解开Optional.None(LLDB) * EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子码=为0x0)



<我有一些ViewController屏幕,其中一个我推出了一个ScrollView,我想让它使用Swift。



我坚持这个:

  import UIKit 

class ViewController:UIViewController {

@IBOutlet var scroller :UIScrollView

覆盖func viewDidLoad(){
super.viewDidLoad()
scroller.scrollEnabled = true;
scroller.contentSize = CGSizeMake(320,624);
//加载视图后进行任何其他设置,通常是从笔尖。
}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}
}

我想如果有人可以提供一个简单的例子如何使用swift做一个scrollview它会解决我的问题。任何帮助都是值得欣赏的。



尝试以旧式风格尝试使用.m和.h文件:



ViewController.m

  #importAmigo-Bridging-Header.h

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320,624)];
//加载视图后进行任何其他设置,通常是从笔尖。
}

- (无效)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

@end

Amigo-Bridging-Header。 h

  #import< UIKit / UIKit.h> 

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

干杯

解决方案

让我们试一试。需要注意的一点是我还没有找到一种方法来将self.view作为UIScrollView进行向下转换,因此你不能像self.view.contentOffset那样进行调用。

  import UIKit 

class ScrollingViewController:UIViewController {
//创建一个scrollView属性,我们将在-loadView中设置为我们的视图
让scrollView = UIScrollView(frame:UIScreen.mainScreen()。bounds)

覆盖func loadView(){
//稍后调用self.view会返回一个UIView!,但我们可以只需调用
// self.scrollView来调整滚动视图的属性:
self.view = self.scrollView

//设置滚动视图
self。 scrollView.contentSize = CGSize(width:1234,height:5678)
// etc ...
}

func example(){
let sampleSubView = UIView ()
self.view.addSubview(sampleSubView)//添加到滚动视图

//不能这样做:
// self.view.contentOffset = CGPoint(x:10,y:20)
//所以我们这样做:
self.scrollView.contentOffset = CGPoint(x:10,y:20)
}

}


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

解决方案

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

}

这篇关于如何使用Swift构建ScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆