简体   繁体   English

在没有XIB文件的情况下将UIView添加到情节提要

[英]Add UIView to Storyboard without a XIB file

I am attempting to add a UIView without a nib file to the storyboard. 我试图将没有nib文件的UIView添加到情节提要。 I have dragged a UIView onto the storyboard and changed it's class to "Jahreskalender" with the Jahreskalender class as follows: 我已将UIView拖到情节提要上,并将其类与Jahreskalender类更改为“ Jahreskalender”,如下所示:

 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. 运行该应用程序时,我得到的只是一个黑色矩形,其中已拖动UIView。 Do I need to load a XIB file or can I do everything with the "Jahreskalender" class programmaticall? 我需要加载XIB文件吗?是否可以使用“ Jahreskalender”类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). 我需要调用init(coder aDecoder:NSCoder)。

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

Now it works. 现在可以了。 Thanks for the tips! 感谢您的提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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