NIB 文件 Swift 中的 ScrollView [英] ScrollView in NIB File Swift

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

问题描述

我正在尝试创建一个 nib 文件,其中视图插座是带有全屏 UIScrollView 的 uiview.如果有意义的话,我想将实际视图添加到滚动视图中(我将附上屏幕截图).我尝试了几种不同的方法,大多数都在 Objective-C 中,但无法使这种方法起作用.当我运行下面的代码时,我在控制台中得到 (lldb),没有其他错误文本.

I'm trying to create a nib file where the view outlet is a uiview with a fullscreen UIScrollView. I want to add the actual view into the scrollview, if that makes sense (I will attach screen shots). I've tried a few different approaches, most being in Objective-C, but can't get this approach working. When i run the code below I get (lldb) in the console with no other error text.

class ScrollViewVC: UIViewController {

@IBOutlet weak var mainScrollView: UIScrollView!
@IBOutlet var contentView: UIView!
@IBOutlet weak var contentViewTitle: UILabel!
@IBOutlet weak var contentViewPrice: UILabel!
@IBOutlet weak var contentViewImageContainer: UIScrollView!

@IBOutlet var imageContainer: UIView!
@IBOutlet weak var imageOne: UIImageView!
@IBOutlet weak var imageTwo: UIImageView!
@IBOutlet weak var imageThree: UIImageView!

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
convenience init() {
    self.init(nibName: "ScrollViewVC", bundle: nil)
}
required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
    super.viewDidLoad()

    self.mainScrollView.addSubview(contentView)
    self.mainScrollView.contentSize = self.contentView.frame.size
    self.contentViewImageContainer.addSubview(self.imageContainer)
    self.contentViewImageContainer.contentSize = self.imageContainer.frame.size
}

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

}

推荐答案

self.mainScrollView.addSubview(contentView)

此行会导致您的应用崩溃.检查您的 contentView 插座指向的位置.它指向在您的 IB 中具有名称主滚动视图"的视图.在里面,如果你有一个滚动视图,你有一个 mainScrollView 出口连接.

this line crashes your app. Check where your contentView outlet points. It points to the view which in your IB has a name "Main Scroll View". Inside if it you have a scroll view to which you have a mainScrollView outlet connection.

因此,您正在尝试将包含此滚动视图的视图添加到滚动视图中.难怪您的应用崩溃了.

So, you are trying to add to the scroll view the view which contains this scroll view. No wonder your app crashes.

对您分配的插座要非常小心.

Be very careful with the outlets which you assign.

这篇关于NIB 文件 Swift 中的 ScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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