[英]Inherit ViewController from Storyboard
我有一个名为CustomComponents.storyboard
的 Storyboard,我在 Storyboard 中设计了BaseViewController
。 例如,我在 Interface Builder .green
BaseViewController
backgroundColor
设置为.green
。 我从BaseViewController
继承了RootViewController
。 我希望RootViewController
的backgroundColor
是.green
。
这是我的RootViewController
class RootViewController: BaseViewController {
}
这是我的BaseViewController
class BaseViewController: UIViewController {
}
这两个类都是空的,我刚刚在 IB 中设置了backgroundColor
。
但是,当我运行应用程序时,我看到白色背景。 但是,当在BaseViewController
编程方式设置颜色时,绿色背景颜色会按预期应用于RootViewController
。
继承在 Storyboard 中设计的BaseViewController
我缺少什么? 基本上,我想在 Storyboard 中设计我的BaseViewController
。 我究竟做错了什么?
只有当您的应用程序最小规范是 iOS 13 时,您才能以编程方式执行此操作,因为新的 API 可用于此目的,因为故事板不包含类声明,它是编码实例的存档
因此,如果您的BaseViewController
具有标识符,则可以使用编码的父级实例化RootViewController
作为
let rootController = UIStoryboard(name: "CustomComponents",
bundle: nil).instantiateViewController(identifier:
"your_base_controller_identifier_here") { coder in
RootViewController(coder: coder)
}
或者如果您的控制器是 root
let rootController = UIStoryboard(name: "CustomComponents",
bundle: nil).instantiateInitialViewController { coder in
RootViewController(coder: coder)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.