ios 如何使用 Swift 构建 ScrollView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24223768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to construct a ScrollView using Swift?
提问by Alex
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?
我正在为 IOS 构建我的第一个应用程序,我正在努力寻找一种方法来使用 XCode6 上的 Swift 代码执行简单的 ScrollView,有人可以帮我找到解决方案吗?
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)
我的问题是我不知道如何使滚动视图在我的代码中工作。我已经把代码放在下面的 ViewController.swift 中,我希望能够在 Main.storyboard 中为 ViewController 选择 Outlet“滚动条”,而不是这个,我收到了错误*"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.
我有一些 ViewController 屏幕,其中一个我放了一个 ScrollView,我想使用 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.
我认为如果有人可以提供一个简单的示例,如何使用 swift 进行滚动视图,它将解决我的问题。任何帮助表示赞赏。
Trying to do it in a old style I tried to do it using a .m and .h file:
尝试以旧样式进行操作时,我尝试使用 .m 和 .h 文件进行操作:
ViewController.m
视图控制器.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
Amigo-Bridging-Header.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIScrollView *scroller;
}
@end
Cheers
干杯
回答by Ryan
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.
让我们试一试。需要注意的一件事是我还没有找到一种方法将 self.view 向下转换为 UIScrollView,所以你不能像 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)
}
}
回答by jrturton
Your outlet is not connected. From the Swift with objective-C book:
您的插座未连接。来自带有 Objective-C的Swift 书:
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
当你在 Swift 中声明一个 outlet 时,编译器会自动将类型转换为一个弱的隐式解包可选项,并为其分配一个初始值 nil。实际上,编译器替换
@IBOutlet var name: Type
为@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.
如果此值未连接,它将保持为 nil,并且在访问该值时会出现运行时错误。
回答by Vinod Joshi
@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);
}