3

This is a near duplicate of this other SO question, however I'm running into some issues that it makes it appear like I'm not doing it correctly. For reference, I followed this excellent tutorial on creating re-usable views from a custom class and xib file (it's only a few minutes :) and I have no problems at all dragging that into another view onto my storyboard (as demonstrated at the end of the video)

Nevertheless for my case — I'm trying to call my custom class programmatically, and add it as a subview to one of my ScrollView instances.

class MainController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        first.directionalLockEnabled = true
        first.pagingEnabled = true
        var item = MyCustomView(frame: CGRectMake(0, 0, self.view.frame.width, 200))
        self.scrollView.addSubview(item)
    }

}

My Custom view looks like this:

import UIKit

class MyCustomView: UIView {
    @IBOutlet var view: UIView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var dressTitle: UILabel!
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        NSBundle.mainBundle().loadNibNamed("MyCustomView", owner: self, options: nil)
        self.view.frame = bounds
        self.addSubview(self.view)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)

    }
}

There is an associated .xib file with it too that has the label and image.

So my question is, my view never appears in my ScrollView. I've double checked the constraints on my scroll view... I can even append simple UIView's with obvious visible dimensions CGRectMake(0, 0, 100, 100)... and nothing ever gives. What am I missing?

8
  • can you print more information on viewDidAppear?? such as, self.view.frame and self.scrollView.frame also the self.scrollView.subViews.count Commented Jul 17, 2015 at 6:48
  • customView = MyCustomView(frame: CGRectZero(top: 0, left: 0, width: 200, height: 50) try like this Commented Jul 17, 2015 at 6:49
  • Yeah, I tried those just in case too :). My main self.view.frame is (0.0, 0.0, 320.0, 568.0) and the self.scrollView.frame is (0.0, 11.0, 320.0, 200.0) Commented Jul 17, 2015 at 6:52
  • @ded check this : youtube.com/watch?v=o3MawJVxTgk Commented Jul 17, 2015 at 6:54
  • @AshishKakkad If you wouldn't mind, please re-read my question carefully — I already referenced that exact video in the original question as the tutorial that I followed. Commented Jul 17, 2015 at 6:58

1 Answer 1

4

With some messing around I accidentally got it to work by duplicating the loadFromNib method to a second initializer in the CustomView.swift file. As seen in the video tutorial posted in the original question it shows just one of the initializers.... but in my case I had to add extra code to the init(frame). Eg:

// once with a NSCoder
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    NSBundle.mainBundle().loadNibNamed("Item", owner: self, options: nil)
    self.view.frame = bounds
    self.addSubview(self.view)
}

// for this to work programmatically I had to do the same...
override init(frame: CGRect) {
    super.init(frame: frame)
    NSBundle.mainBundle().loadNibNamed("Item", owner: self, options: nil)
    self.view.frame = bounds
    self.addSubview(self.view)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.