IBDesignable来自外部框架? [英] IBDesignable from External Framework?

查看:103
本文介绍了IBDesignable来自外部框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一些使用 @IBDesignable @IBInspectable 标记的自定义视图。我已将这些添加到我的框架中,然后将我的框架链接到我的测试应用程序。但是Designables从未出现在StoryBoard中。

I'd like to create some custom views that use the @IBDesignable and @IBInspectable tags. I have added these to my Framework and then linked my Framework to my test application. But the Designables never show up in the StoryBoard.

我如何使用 @IBDesignable @IBInspectable 从外部框架创建自定义视图?

How can I use @IBDesignable and @IBInspectable to create custom views from an external framework?

可以使用 @IBDesignable @IBInspectable

谢谢。

推荐答案

我找到了一种在Cocoa Touch框架中使用designables和inspectables的方法。下面的说明适用于Objective-C项目和Xcode 8(我没有在旧版本上测试),如果涉及Swift代码,则应该相同。

I have found a way to use designables and inspectables with Cocoa Touch frameworks. The instructions below are for an Objective-C project and Xcode 8 (I didn't test on older versions), and should be identical if Swift code is involved.

自Interface Builder在框架中不会发现可设计项,在框架标题中将类标记为 IB_DESIGNABLE 是没用的。 Interface Builder将仅在编译项目源文件时发现可设计的类。因此,我们的想法是将这些信息作为框架伴随源文件提供,然后客户可以使用他们的项目进行编译。

Since designables are not discovered by Interface Builder in frameworks, it is useless to mark classes as IB_DESIGNABLE in the framework headers. Interface Builder will only discover designable classes when compiling project source files. The idea is therefore to supply this information as a framework companion source file, which clients can then compile with their project.

我发现你没有子类来标记一个可在项目中设计的框架类。你可以简单地注释每个必须通过伴随 .m 源文件中声明的类别来设计的类,例如:

I discovered that you do not have to subclass to mark a framework class as designable in a project. You can namely simply annotate each class which must be designable through a category declared in the companion .msource file, e.g.:

IB_DESIGNABLE
@interface MyCustomView (Designable)

@end

事实上,代码甚至不需要编译,你可以将它包装在一个封闭的 #if 0 ... #endif 它仍然有效。所需要的只是该类以某种方式与 IB_DESIGNABLE 属性相关联。

In fact, the code does not even have to compile, you could wrap it within an enclosing #if 0 ... #endif and it would still work. All that is needed is that the class is somehow associated with the IB_DESIGNABLE attribute.

考虑到这些信息,以下是如何使Designables与Cocoa Touch框架一起使用:

With this information in mind, here is how to make designables work with Cocoa Touch frameworks:

如果您是框架供应商:


  1. 如果需要,请使用必须可设计的组件 -prepareForInterfaceBuilder

  2. 将一个文件夹引用(蓝色文件夹)添加到您的框架目标,其中包含随附的 .m 文件。可能的命名约定是将文件夹命名为 Designables ,并将其中的文件命名为
    MyFrameworkNameDesignables.m ,但你可以选择你最喜欢的。

  3. .m 文件中,为每个必须可设计的视图创建类似上面的类别。文件本身必须由客户端项目编译,这意味着您需要进行必要的导入(例如,您的框架全局公共标题 #import< MyFramework / MyFramework.h> )或使用 #if 0 ... #endif 上面的技巧

  1. If needed, have the component which must be designable implement -prepareForInterfaceBuilder
  2. Add a Folder reference (blue folder) to your framework target, with the companion .m file in it. A possible naming convention would be to name the folder Designables and the file within it MyFrameworkNameDesignables.m, but you can choose whatever you like most.
  3. In the .m file, create a category like the one above for each view which must be designable. The file itself must be compilable by client projects, which means you either need to make the necessary imports (e.g. your framework global public header #import <MyFramework/MyFramework.h>) or use the #if 0 ... #endif trick above

通过将文件封装在蓝色文件夹中,我们确保在最终的 .framework 产品中复制文件夹,而不编译伴随源文件。此外,由于该文件夹是框架包的一部分,它可供框架的所有客户使用,无论是直接集成还是使用Carthage。

By enclosing the file within a blue folder, we ensure the folder is copied as is in the final .framework product, without the companion source file being compiled. Moreover, as the folder is part of the framework bundle, it is available for all clients of the framework, whether they integrate it directly or using Carthage.

如果您有使用框架作为目标依赖项的演示项目,如果您的框架依赖于其他框架,那么在尝试在演示项目中呈现可设计视图时,您将遇到 dlopen 问题。这是因为在框架目标中发现了 IB_DESIGNABLE 属性(因为已经添加了 Designables 文件夹), Xcode预构建在与您的项目对应的 Build / Intermediates / IBDesignables 派生数据文件夹中。如果查看此文件夹的内容,则缺少框架依赖项,导致 dlopen 问题。

If you have a demo project using the framework as target dependency, and if your framework depends on other frameworks, you will run into dlopen issues when trying to render designable views in the demo project. This is because IB_DESIGNABLE attributes are discovered in the framework target (since the Designables folder has been added to it), which Xcode pre-builds in the Build/Intermediates/IBDesignables derived data folder corresponding to your project. If you look at the content of this folder, framework dependencies are missing, leading to the dlopen issues.

To修复演示中的渲染,只需将 Copy Files 阶段添加到框架目标,将每个必需的框架依赖项添加到文件列表,并将 Products目录设置为目标。现在,当Xcode构建用于渲染的演示时,它也将包含依赖项。

To fix rendering in your demo, simply add a Copy Files phase to the framework target, add each required framework dependency to the file list, and set Products directory as destination. Now, when Xcode builds your demo for rendering, it will include the dependencies as well.

如果你是具有可设计支持的框架的用户:


  • 将框架(及其所有框架依赖项,如果有的话)添加为嵌入式二进制到目标

  • 从框架包中检索伴随源文件,然后复制到项目中,并将其添加到目标中。添加位于框架内的文件或使用符号链接遗憾地不起作用,因为Xcode似乎根本不在框架内查找

  • 添加可设计视图类的实例( MyCustomView 在上面的示例中)到故事板。 Interface Builder应该构建项目并呈现视图

  • Add the framework (and all its framework dependencies, if any) as embedded binary to your target
  • Retrieve the companion source file from the framework bundle and copy it into your project, adding it to your target. Adding the file located within the framework or using a symbolic link sadly does not work, as Xcode does not seem to look within frameworks at all
  • Add an instance of the designable view class (MyCustomView in our example above) to a storyboard. Interface Builder should build the project and render the view

此解决方案并不完美,因为您仍然需要手动复制提供的源文件,这可以在框架版本之间改变。但它的效果非常好,并提供了框架包本身所需的一切。

This solution is not perfect since you still have to manually copy a supplied source file, which could change between framework versions. But it works pretty well, though, and provides everything required within the framework bundle itself.

这篇关于IBDesignable来自外部框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆