问题
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