如何在单独的 xib 文件中创建工具栏并单独 class 并在多个视图控制器中使用它?

How to create toolbar in separate xib file and separate class and use it in multiple view controllers?

如何在分离的 xib 文件中创建工具栏并分离 class 并在多个视图控制器中使用它?

基本上,您问的是是否可以 re-use 在不同的 nib 或视图中创建一个在 nib 中创建的视图。

答案是肯定的,也不是: 你可以做到,但你必须以编程方式进行。 我们将要重用的视图称为 View a,将要插入的视图称为 View B。您需要在代码中实例化View A并将其作为子视图添加到View B。如果您也为 View b 使用 XIB 文件,您可以在该 nib 文件中添加一个 "container view",并在代码中添加 A 作为容器的子视图。 (给它容器的边界)。

let reusableView = Bundle.main.loadNibNamed("ResuableView", owner: self, options: nil)?.first as! ResuableView
resuableView.frame = self.containerView.bounds
self.containerView.addSubview(resuableView)