ios - 在没有 XIB 文件的情况下将 UIView 添加到 Storyboard

标签 ios swift

我正在尝试将没有 nib 文件的 UIView 添加到 Storyboard 中。我已将 UIView 拖到 Storyboard 上,并将其类更改为“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)
   }
}

当我运行该应用程序时,我得到的只是一个黑色矩形,我在其中拖动了 UIView。 我是否需要加载 XIB 文件,或者我可以使用“Jahreskalender”类编程来做所有事情吗?到处寻找,但找不到任何教程。

最佳答案

好吧,经过更多的尝试和错误,我发现了我的错误。我需要调用 init(coder aDecoder: NSCoder)。

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

现在可以了。感谢您的提示!

关于ios - 在没有 XIB 文件的情况下将 UIView 添加到 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30638044/

相关文章:

ios - 如何解决这个 Collection View 和数组问题?

ios - Xcode 6 : iOS doesn't use storyboard localizations

ios - 如何从这个应用程序复制UIButton选择的设计和 Action ?

ios - 在 Swift 中使用 UIButton 制作图像选择器

ios - 在 RxSwift 中修改 BehaviourRelay 数组内部的属性

ios - 在 gamescene.sks 中创建带边界的空节点

ios - 具有相同 xib 的两个不同的 UITableViewCell

ios - iPhone 应用程序启动时无法从 Watch 读取 NSUserDefaults

ios - SLComposeViewController 中的 108 个字符

iOS 相机通过浏览器访问。覆盖?