美文网首页
39.打包包含xib的framework库

39.打包包含xib的framework库

作者: noonez | 来源:发表于2017-02-27 11:46 被阅读39次

平时我们加载xib都是使用Bunlde.main,这样做是没错的,因为我们使用的xib就在主项目的Bundle中。
比如我现在加载一个叫"FView"的xib,FView是个UIView

func addXib() {
        let views = Bundle.main.loadNibNamed("FView", owner: self, options: nil)!
        let tmp = views.first as! UIView
        tmp.frame.size = CGSize(width: 300, height: 300)
        view.addSubview(tmp)
 }

但是如果是在要发布的framework库中,还这样加载xib,那么别的地方使用这个framework就会引起崩溃。因为库中的xib并不在主项目的Bunlde中,而是在framework库中的Bundle中。
要正确加载xib,只需要将加载xib的地方改下,加载时改用当前项目的Bundle,这样app就不会去Bundle.main中找xib了。

let views = Bundle(for: type(of:self)).loadNibNamed("TestView", owner: self, options: nil)
        let testView = views?.first as! UIView
        testView.frame.size.width = self.frame.width
        addSubview(testView)

这是Bundle构造函数描述

Bundle.init(for aClass: AnyClass)

Description

Returns the NSBundle object with which the specified class is associated.(返回与指定的类相关联的NSBundle对象。)

源码:https://github.com/ghyh22/XibFramework

相关文章

  • 39.打包包含xib的framework库

    平时我们加载xib都是使用Bunlde.main,这样做是没错的,因为我们使用的xib就在主项目的Bundle中。...

  • iOS 包含资源文件的Bundle

    framework 和 .a 静态库只能包含头文件和代码,不能包含图片,XIB,SB 等这种资源文件。,那么如果我...

  • 制作一个bundle

    前言 之前已经介绍了打包通用静态库,详情见打包通用静动态库。接下来制作一下包含图片,xib等资源的bundle。 ...

  • iOS 制作 .framework

    用xcode制作 .framework 的步骤,里面包含 xib。image。和第三方sdk或者开源库的处理。 第...

  • 最新打包framework的血与泪

    参考文章:iOS 项目打包framework生成静态Framework(包含界面、图片、第三方库、第三方frame...

  • iOS framework 简单笔记

    1. framework动态库可以包含静态库和bundle资源文件 2. framework静态库不能包含动态库 ...

  • 目录一、库二、静态库、动态库、Framework三、打包静态库 1、.a静态库和.framework静态库的区别 ...

  • 打包Framework,使用bundle,Framework联调

    文章主要介绍怎么打包静态库(这里只说Framework),打包bundle,Framework联调,以及项目中遇到...

  • iOS创建.framework文件

    iOS的库分为 .a 纯静态库只能包含代码 .dylib 动态库 .framework 包含动态库和静态库,可...

  • flutter build ios-framework 打包为静

    App.framework 和 flutter.framework,包含其他资源文件,无法打包为static fr...

网友评论

      本文标题:39.打包包含xib的framework库

      本文链接:https://www.haomeiwen.com/subject/oekzwttx.html