ScrollView in NIB File Swift

↘锁芯ラ 提交于 2019-12-12 03:46:33

问题


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

}


回答1:


self.mainScrollView.addSubview(contentView)

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.



来源:https://stackoverflow.com/questions/32172893/scrollview-in-nib-file-swift

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