iOS-swiftiOS DeveloperIOS知识积累

iOS | 在framework中打包和使用bundle

2018-03-13  本文已影响170人  清無

要求

分析

实例

以封装一个IconButton自定义控件为例

注意:这个Bundle中主要存放Framework中的xib等资源文件。

将swift文件放入IconButton文件夹下 将xib文件放入IconButtonBundle文件夹下
import UIKit

public final class IconButtonItem: UIView{
    public enum Position: UInt {
        case left = 0, right = 1, top = 2, bottom = 3
    }
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var iconImageView: UIImageView!
    
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        
        backgroundColor = .clear
    }
    
    static func item(withFrame frame: CGRect = .zero,
                       icon: UIImage?,
                       title: String?,
                       iconPosition position: Position) -> IconButtonItem{
        guard
            let path = Bundle.main.path(forResource: "Frameworks/IconButton.framework/IconButtonBundle", ofType: "bundle"),
            let bundle = Bundle(path: path),
            let xibs = bundle.loadNibNamed("IconButton", owner: nil, options: nil),
            let button = xibs[Int(position.rawValue)] as? IconButtonItem else{
            return IconButtonItem()
        }
        button.frame = frame
        button.titleLabel.text = title
        button.iconImageView.image = icon
        return button
    }
}

@IBDesignable
public final class IconButton: UIView {
    
// MARK: - IBOutlets
    @IBInspectable
    public var iconPosition: UInt = 0{
        didSet{
            iconPosition = iconPosition > 3 ? 3 : iconPosition
            makeButtonItem()
        }
    }
    @IBInspectable
    public var iconImage: UIImage? = nil{
        didSet{
            item.iconImageView.image = iconImage
        }
    }
    @IBInspectable
    public var title: String? = nil{
        didSet{
            item.titleLabel.text = title
        }
    }
    
    private var item: IconButtonItem!
    
// MARK: - Properties
    
// MARK: - Initial Method
    private func setupUI() {
        item.titleLabel.text = title
        item.iconImageView.image = iconImage
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        makeButtonItem()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        makeButtonItem()
    }
 
// MARK: - Lifecycle Method
    override public func awakeFromNib() {
        super.awakeFromNib()        
        setupUI()
    }
    
// MARK: - Action & IBOutletAction
    
// MARK: - Override Method
    
// MARK: - Private method
    private func makeButtonItem(){
        item?.removeFromSuperview()
        item = IconButtonItem.item(withFrame: bounds, icon: iconImage, title: title, iconPosition: IconButtonItem.Position(rawValue: iconPosition)!)
        item.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(item)
    }

// MARK: - Public Method
}

当然,本文重点要演示的是如何在.framework中打包.bundle文件,所以以上IconButton实现代码不做解释,相信读者很容易理解。

编译IconButtonBundle 编译成功会生成IconButtonBundle.bundle文件 Debug 这样每次改动重新编译生成.bundle,都会被自动引入到framework中
// 先拿到.bundle文件在被使用工程中的实际位置,
// 一般是在Bundle.main(使用者工程路径)下的Frameworks文件夹下的xxx.framwork下
let path = Bundle.main.path(forResource: "Frameworks/IconButton.framework/IconButtonBundle", ofType: "bundle")

// 创建资源bundle实例对象
let bundle = Bundle(path: path)

// 加载nib文件(一个xib文件中可能有多个nib文件)
let xibs = bundle.loadNibNamed("IconButton", owner: nil, options: nil)

// 根据传入的position值,加载对应的nib控件
let button = xibs[Int(position.rawValue)] as? IconButtonItem
UIImage(named: "xxx", in: bundle, compatibleWith: nil)
将IconButton.framework加入工程中 在Storyboard上使用 可视化的配置属性
class ViewController: UIViewController {
    @IBOutlet weak var iconButton: IconButton!

    @IBAction func positionChanged(_ sender: UISegmentedControl) {
        iconButton.iconPosition = UInt(sender.selectedSegmentIndex)
    }
}
最终效果

说明:当然,你可以手动替换.framework中的.bundle文件中的图片等资源,这里不做演示了。

.bundle中内容

Carthage

Manage Schemes 勾选shared
cd 工程目录
carthage build --no-skip-current
编译成功

关于更多Carthage,可以参考我这篇文章https://www.jianshu.com/p/76b9ff09f99c

Github

https://github.com/BackWorld/DemoBundleFramework

以上就是简单演示了如何生成并使用一个含有bundle文件的framework,如果对你有帮助,欢迎加关注和点赞👍~

上一篇 下一篇

猜你喜欢

热点阅读