Pete
Pete

Reputation: 613

Add UIView to Storyboard without a XIB file

I am attempting to add a UIView without a nib file to the storyboard. I have dragged a UIView onto the storyboard and changed it's class to "Jahreskalender" with the Jahreskalender class as follows:

 class Jahreskalender: UIView {

 override func drawRect(rect: CGRect) {
     // Drawing code
     self.opaque = false
     self.backgroundColor = UIColor(red: 100, green: 1, blue: 5, alpha: 0)
     let jahresView = UIImageView(image: UIImage(named: "Calender.png"))
     jahresView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
     self.addSubview(jahresView)
   }
}

When I run the app all I get is a black rectangle where I have dragged the UIView. Do I need to load a XIB file or can I do everything with the "Jahreskalender" class programmaticall? Looked everywhere but can't find any tutorials.

Upvotes: 3

Views: 693

Answers (1)

Pete
Pete

Reputation: 613

Well, after some more trial and error I found my mistake. I need to call init(coder aDecoder: NSCoder).

 required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.opaque = false
        }

Now it works. Thanks for the tips!

Upvotes: 1

Related Questions