简体   繁体   中英

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM