从故事板中的外部 xib 文件加载视图
我想在故事板中的多个视图控制器中使用视图。因此,我考虑在外部 xib 中设计视图,以便更改反映在每个视图控制器中。但是如何从故事板中的外部 xib 加载视图?这是否可能?如果不是这种情况,还有哪些其他替代方案可以满足上述情况?
I want to use a view throughout multiple viewcontrollers in a storyboard. Thus, I thought about designing the view in an external xib so changes are reflected in every viewcontroller. But how can one load a view from a external xib in a storyboard and is it even possible? If thats not the case, what other alternatives are availble to suit the situation abouve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我的完整示例位于此处,但我将在下面提供摘要。
布局
将同名的 .swift 和 .xib 文件添加到您的项目中。 .xib 文件包含您的自定义视图布局(最好使用自动布局约束)。
使 swift 文件成为 xib 文件的所有者。
代码
将以下代码添加到 .swift 文件中,并连接 .xib 文件中的出口和操作。
使用它
在故事板的任何位置使用您的自定义视图。只需添加一个
UIView
并将类名称设置为您的自定义类名称。My full example is here, but I will provide a summary below.
Layout
Add a .swift and .xib file each with the same name to your project. The .xib file contains your custom view layout (using auto layout constraints preferably).
Make the swift file the xib file's owner.
Code
Add the following code to the .swift file and hook up the outlets and actions from the .xib file.
Use it
Use your custom view anywhere in your storyboard. Just add a
UIView
and set the class name to your custom class name.有一段时间,Christopher Swasey 的方法是我发现的最好方法。我向团队中的几位高级开发人员询问了这个问题,其中一位拥有完美的解决方案!它满足了 Christopher Swasey 如此雄辩地解决的每一个问题,并且不需要样板子类代码(我主要关心的是他的方法)。有一个问题,但除此之外,它相当直观且易于实现。
MyCustomClass.swift
MyCustomClass.xib
File's Owner
设置为您的自定义类 (MyCustomClass
)class
值(在identity Inspector
下)留空。 因此您的自定义视图将没有指定的类,但它将具有指定的文件所有者。助理编辑器
一样连接您的插座。Connections Inspector
,您会发现您的引用插座没有引用您的自定义类(即MyCustomClass
),而是引用文件的所有者
。由于File's Owner
被指定为您的自定义类,因此插座将连接并正常工作。NibLoadable
协议。.swift
文件名与.xib
文件名不同,请将nibName
属性设置为.xib
文件的名称。必需的 init?(coder aDecoder: NSCoder)
并覆盖 init(frame: CGRect)
以调用setupFromNib()
,如下例所示。MyCustomClass
)。以下是您要引用的协议:
下面是实现该协议的
MyCustomClass
示例(.xib 文件名为MyCustomClass.xib
):注意:如果您错过了陷阱并将 .xib 文件中的
class
值设置为自定义类,那么它将不会在情节提要中绘制,并且您将收到EXC_BAD_ACCESS
错误当你运行应用程序,因为它陷入了尝试使用init?(coder aDecoder: NSCoder)
方法从 nib 初始化类的无限循环,然后调用Self.nib.instantiate< /code> 并再次调用
init
。For a while Christopher Swasey's approach was the best approach I had found. I asked a couple of the senior devs on my team about it and one of them had the perfect solution! It satisfies every one of the concerns that Christopher Swasey so eloquently addressed and it doesn't require boilerplate subclass code(my main concern with his approach). There is one gotcha, but other than that it is fairly intuitive and easy to implement.
MyCustomClass.swift
MyCustomClass.xib
File's Owner
of the .xib file to be your custom class (MyCustomClass
)class
value (under theidentity Inspector
) for your custom view in the .xib file blank. So your custom view will have no specified class, but it will have a specified File's Owner.Assistant Editor
.Connections Inspector
you will notice that your Referencing Outlets do not reference your custom class (i.e.MyCustomClass
), but rather referenceFile's Owner
. SinceFile's Owner
is specified to be your custom class, the outlets will hook up and work propery.NibLoadable
protocol referenced below..swift
file name is different from your.xib
file name, then set thenibName
property to be the name of your.xib
file.required init?(coder aDecoder: NSCoder)
andoverride init(frame: CGRect)
to callsetupFromNib()
like the example below.MyCustomClass
).Here is the protocol you will want to reference:
And here is an example of
MyCustomClass
that implements the protocol (with the .xib file being namedMyCustomClass.xib
):NOTE: If you miss the Gotcha and set the
class
value inside your .xib file to be your custom class, then it will not draw in the storyboard and you will get aEXC_BAD_ACCESS
error when you run the app because it gets stuck in an infinite loop of trying to initialize the class from the nib using theinit?(coder aDecoder: NSCoder)
method which then callsSelf.nib.instantiate
and calls theinit
again.假设您已经创建了一个要使用的 xib:
1)创建 UIView 的自定义子类(您可以转到 File -> New -> File... -> Cocoa Touch Class。确保“Subclass” of:”是“UIView”)。
2) 在初始化时添加一个基于xib 的视图作为该视图的子视图。
在 Obj-C 中
在 Swift 2 中
在 Swift 3 中
3) 无论您想在情节提要中的何处使用它,都像往常一样添加 UIView,选择新添加的视图,转到 Identity Inspector(右上角的第三个图标,看起来像一个带有线条的矩形),然后在“自定义类”下的“类”中输入子类的名称。
Assuming that you've created an xib that you want to use:
1) Create a custom subclass of UIView (you can go to File -> New -> File... -> Cocoa Touch Class. Make sure "Subclass of:" is "UIView").
2) Add a view that's based on the xib as a subview to this view at initialization.
In Obj-C
In Swift 2
In Swift 3
3) Wherever you want to use it in your storyboard, add a UIView as you normally would, select the newly added view, go to the Identity Inspector (the third icon on the upper right that looks like a rectangle with lines in it), and enter your subclass's name in as the "Class" under "Custom Class".
我一直发现“将其添加为子视图”解决方案并不令人满意,因为它与 (1) 自动布局、(2) @IBInspectable 和 (3) 出口相混淆。相反,让我向您介绍
awakeAfter:
的魔力,这是一个NSObject
方法。awakeAfter
允许您将实际从 NIB/Storyboard 唤醒的对象完全替换为不同的对象。然后,该对象将经历水合过程,调用 awakeFromNib,将其添加为视图等。我们可以在“纸板剪纸”中使用它我们视图的子类,其唯一目的是从 NIB 加载视图并将其返回以在 Storyboard 中使用。然后,在 Storyboard 视图的身份检查器中指定可嵌入的子类,而不是原始类。它实际上不必是子类才能工作,但使其成为子类才允许 IB 查看任何 IBInspectable/IBOutlet 属性。
这个额外的样板可能看起来不是最理想的——从某种意义上来说确实如此,因为理想情况下
UIStoryboard
会无缝地处理这个问题——但它的优点是完全保留原始的 NIB 和UIView
子类未经修改。它所扮演的角色基本上是适配器或桥接类,并且作为附加类在设计方面是完全有效的,即使它令人遗憾。另一方面,如果您喜欢对类进行节约,@BenPatch 的解决方案可以通过实现一个带有一些其他细微更改的协议来工作。哪种解决方案更好的问题归结为程序员风格的问题:是喜欢对象组合还是多重继承。注意:NIB 文件中视图上设置的类保持不变。可嵌入子类仅在故事板中使用。子类不能用于在代码中实例化视图,因此它本身不应该有任何额外的逻辑。它应该仅包含
awakeAfter
钩子。⚠️ 这里的一个显着缺点是,如果您在情节提要中定义与另一个视图无关的宽度、高度或纵横比约束,则必须手动复制它们。关联两个视图的约束被安装在最近的共同祖先上,并且视图从故事板中由内而外地被唤醒,因此当这些约束在超级视图上被水合时,交换已经发生了。仅涉及相关视图的约束直接安装在该视图上,因此在交换发生时会被丢弃,除非它们被复制。
请注意,这里发生的情况是将故事板中的视图上安装的约束复制到新实例化的视图,该视图可能已经有自己的约束,在其笔尖中定义文件。那些不受影响。
instantiateViewFromNib
是UIView
的类型安全扩展。它所做的只是循环遍历 NIB 的对象,直到找到与类型匹配的对象。请注意,泛型类型是返回值,因此必须在调用站点指定类型。I've always found the "add it as a subview" solution unsatisfactory, seeing as it screws with (1) autolayout, (2)
@IBInspectable
, and (3) outlets. Instead, let me introduce you to the magic ofawakeAfter:
, anNSObject
method.awakeAfter
lets you swap out the object actually woken up from a NIB/Storyboard with a different object entirely. That object is then put through the hydration process, hasawakeFromNib
called on it, is added as a view, etc.We can use this in a "cardboard cut-out" subclass of our view, the only purpose of which will be to load the view from the NIB and return it for use in the Storyboard. The embeddable subclass is then specified in the Storyboard view's identity inspector, rather than the original class. It doesn't actually have to be a subclass in order for this to work, but making it a subclass is what allows IB to see any IBInspectable/IBOutlet properties.
This extra boilerplate might seem suboptimal—and in a sense it is, because ideally
UIStoryboard
would handle this seamlessly—but it has the advantage of leaving the original NIB andUIView
subclass completely unmodified. The role it plays is basically that of an adapter or bridge class, and is perfectly valid, design-wise, as an additional class, even if it is regrettable. On the flip side, if you prefer to be parsimonious with your classes, @BenPatch's solution works by implementing a protocol with some other minor changes. The question of which solution is better boils down to a matter of programmer style: whether one prefers object composition or multiple inheritance.Note: the class set on the view in the NIB file remains the same. The embeddable subclass is only used in the storyboard. The subclass can't be used to instantiate the view in code, so it shouldn't have any additional logic, itself. It should only contain the
awakeAfter
hook.⚠️ The one significant drawback here is that if you define width, height, or aspect ratio constraints in the storyboard that don't relate to another view then they have to be copied over manually. Constraints that relate two views are installed on the nearest common ancestor, and views are woken from the storyboard from the inside-out, so by the time those constraints are hydrated on the superview the swap has already occurred. Constraints that only involve the view in question are installed directly on that view, and thus get tossed when the swap occurs unless they are copied.
Note that what is happening here is constraints installed on the view in the storyboard are copied to the newly instantiated view, which may already have constraints of its own, defined in its nib file. Those are unaffected.
instantiateViewFromNib
is a type-safe extension toUIView
. All it does is loop through the NIB's objects until it finds one that matches the type. Note that the generic type is the return value, so the type has to be specified at the call site.尽管最受欢迎的答案效果很好,但它们在概念上是错误的。它们都使用
File'sowner
作为类的出口和UI组件之间的连接。文件所有者
应该仅用于顶级对象,而不是UIView
。请查看 Apple 开发者文档。将 UIView 作为
文件的所有者
会导致这些不良后果。self
的地方使用contentView
。它不仅丑陋,而且在结构上也是错误的,因为中间视图使数据结构无法传达其 UI 结构。这就像违背声明式 UI 的概念。有一种优雅的方法可以在不使用
文件所有者
的情况下完成此操作。请查看此博客文章。它解释了如何以正确的方式进行操作。Although the top most popular answers works fine, they are conceptually wrong. They all use
File's owner
as connection between class's outlets and UI components.File's owner
is supposed to be used only for top-level objects notUIView
s. Check out Apple developer document.Having UIView as
File's owner
leads to these undesirable consequences.contentView
where you are supposed to useself
. It’s not only ugly, but also structurally wrong because the intermediate view keeps data structure from conveying it’s UI structure. It's like going against the concept of declarative UI.There's elegant way to do it without using
File's owner
. Please check this blog post. It explains how to do it the right way.我认为使用 XIB 视图的替代方案是在单独的故事板中使用视图控制器。
然后在主情节提要中代替自定义视图,使用
容器视图
和Embed Segue
,并使用StoryboardReference
到这个自定义视图控制器 哪个视图应放置在主情节提要中的其他视图内。然后我们可以通过准备segue在该嵌入ViewController和主视图控制器之间设置委托和通信。这种方法与显示 UIView 不同,但可以利用更简单、更高效(从编程角度来看)来实现相同的目标,即拥有在主情节提要中可见的可重用自定义视图。
额外的优点是您可以在 CustomViewController 类中实现逻辑,并设置所有委托和视图准备,而无需创建单独的(在项目中更难找到)控制器类,也无需使用 Component 将样板代码放置在主 UIViewController 中。我认为这对于可重复使用的组件很有好处。可嵌入其他视图的音乐播放器组件(类似小部件)。
I think about
alternative
for usingXIB views
to be usingView Controller
in separate storyboard.Then in main storyboard in place of custom view use
container view
withEmbed Segue
and haveStoryboardReference
to this custom view controller which view should be placed inside other view in main storyboard.Then we can set up delegation and communication between this embed ViewController and main view controller through prepare for segue. This approach is different then displaying UIView, but much simpler and more efficiently (from programming perspective) can be utilised to achieve the same goal, i.e. have reusable custom view that is visible in main storyboard
The additional advantage is that you can implement you logic in CustomViewController class and there set up all delegation and view preparation without creating separate (harder to find in project) controller classes, and without placing boilerplate code in main UIViewController using Component. I think this is good for reusable components ex. Music Player component (widget like) that is embeddable in other views.
目前最好的解决方案是只使用自定义视图控制器,其视图在 xib 中定义,然后简单地删除 Xcode 在向其添加视图控制器时在情节提要中创建的“view”属性(不要忘记设置不过自定义类)。
这将使运行时自动查找 xib 并加载它。您可以将此技巧用于任何类型的容器视图或内容视图。
Best solution currently is to just use a custom view controller with its view defined in a xib, and simply delete the "view" property that Xcode creates inside the storyboard when adding the view controller to it (don't forget to set the name of the custom class though).
This will make the runtime automatically look for the xib and load it. You can use this trick for any kind of container views, or content view.
根据 Ben Patch 的回复 中描述的步骤解决 Objective-C。
使用 UIView 扩展:
创建文件
MyView.h
、MyView.m
和MyView.xib
。首先准备您的
MyView.xib
,正如 Ben Patch 的回复 所说,所以 set classMyView< /code> 表示文件的所有者,而不是此 XIB 内的主视图。
MyView.h
:MyView.m
:然后以编程方式创建视图:
警告! 如果满足以下条件,则该视图的预览将不会显示在 Storyboard 中:由于 Xcode >= 9.2 中的此错误,您使用 WatchKit 扩展: https://forums.developer.apple.com/thread/95616
Solution for Objective-C according to steps described in Ben Patch's response.
Use extension for UIView:
Create files
MyView.h
,MyView.m
andMyView.xib
.First prepare your
MyView.xib
as Ben Patch's response says so set classMyView
for File's owner instead of main view inside this XIB.MyView.h
:MyView.m
:And later just create your view programatically:
Warning! Preview of this view will not be shown in Storyboard if you use WatchKit Extension because of this bug in Xcode >= 9.2: https://forums.developer.apple.com/thread/95616
这就是您一直想要的答案。您只需创建您的
CustomView
类,将其主实例与所有子视图和出口一起放在 xib 中。然后,您可以将该类应用于故事板或其他 xib 中的任何实例。无需摆弄文件的所有者,或将出口连接到代理或以特殊方式修改 xib,或添加自定义视图的实例作为其自身的子视图。
操作:
UIView
更改为NibView
(或从UITableViewCell
更改为NibTableViewCell
)只需执行以下 它!
它甚至可以与 IBDesignable 配合使用,在设计时在故事板中引用您的自定义视图(包括 xib 中的子视图)。
您可以在这里阅读更多相关信息:
https://medium. com/build-an-app-like-lego/embed-a-xib-in-a-storyboard-953edf274155
你可以获得开源BFWControls 框架在这里:
https://github.com/BareFeetWare/BFWControls
这是
NibReplaceable 的简单摘录
驱动它的代码,如果你好奇的话:https://gist.github.com/barefeettom/f48f6569100415e0ef1fd530ca39f5b4汤姆
Here's the answer you've wanted all along. You can just create your
CustomView
class, have the master instance of it in a xib with all the subviews and outlets. Then you can apply that class to any instances in your storyboards or other xibs.No need to fiddle with File's Owner, or connect outlets to a proxy or modify the xib in a peculiar way, or add an instance of your custom view as a subview of itself.
Just do this:
UIView
toNibView
(or fromUITableViewCell
toNibTableViewCell
)That's it!
It even works with IBDesignable to refer your custom view (including the subviews from the xib) at design time in the storyboard.
You can read more about it here:
https://medium.com/build-an-app-like-lego/embed-a-xib-in-a-storyboard-953edf274155
And you can get the open source BFWControls framework here:
https://github.com/BareFeetWare/BFWControls
And here's a simple extract of the
NibReplaceable
code that drives it, in case you're curious: https://gist.github.com/barefeettom/f48f6569100415e0ef1fd530ca39f5b4Tom ????
即使您的类与 XIB 的名称不同,也可以使用此解决方案。
例如,如果您有一个基本视图控制器类controllerA,它具有一个XIB名称controllerA.xib,并且您使用controllerB对其进行了子类化,并且想要在故事板中创建controllerB的实例,那么您可以:
*
This solution can be used even if your class does not have the same name as the XIB.
For example, if you have a base view controller class controllerA which has a XIB name controllerA.xib and you subclassed this with controllerB and want to create an instance of controllerB in a storyboard, then you can:
*
类 BYTXIBView: UIView {
var nibView: UIView?
}
class BYTXIBView: UIView {
var nibView: UIView?
}