0

I'm developing my first pod file for a custom toast. I have designed the UIView in a .xib file. This works fine in the initial project, but when I copy those files to pod project, it shows

Thread 1: Exception: "Could not load NIB in bundle: 'NSBundle </Users/sameernawaz/Library/Developer/CoreSimulator/Devices/9FD32443-4431-480F-93B3-E1106D74E944/data/Containers/Bundle/Application/05EFC4C0-A60C-4A72-B23D-8575F0218B64/MotionToastView_Example.app> (loaded)' with name 'MTVibrant'"

I have tried adding these .xib files target -> copy bundle resource, but nothing helps. I'm not sure am I allowed to use xib files or should I code my UIView in Swift.

import UIKit

class MTVibrant: UIView {
    
    @IBOutlet weak var headLabel: UILabel!
    @IBOutlet weak var msgLabel: UILabel!
    @IBOutlet weak var circleImg: UIImageView!
    @IBOutlet weak var toastView: UIView!
    @IBOutlet weak var circleView: UIView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    
    func commonInit() {
        let viewFromXib = Bundle.main.loadNibNamed("MTVibrant", owner: self, options: nil)![0] as! UIView
        viewFromXib.frame = self.bounds
        addSubview(viewFromXib)
    }
}

Sameer Nawaz
  • 101
  • 10
  • `Bundle.main`, That's where lies your error. Your View is inside the Pod bundle, not your main app bundle. https://stackoverflow.com/questions/44086009/path-to-bundle-of-ios-framework – Larme Aug 11 '20 at 15:41
  • Does this answer your question? [Path to bundle of iOS framework](https://stackoverflow.com/questions/44086009/path-to-bundle-of-ios-framework) – Larme Aug 11 '20 at 15:42
  • But still it is called by my own pod file, not by end project. The developer calls my ToastMethod, it shows up. He passes arguments and once he calls this, it goes in my pod ExtensionFile and calls this UIView. Or am I doing this right? `window.addSubview(toastView!) UIView.animate(withDuration: 1.0, delay: toastDuration, animations: { toastView!.alpha = 0 }) { (_) in toastView!.removeFromSuperview() }` This code is inside pod. The toastView is the above UIView – Sameer Nawaz Aug 11 '20 at 15:51

1 Answers1

0

Swift 5 Load bundle for: rather than Bundle.main

let bundle = Bundle(for: CURRENT_UIVIEW_CLASSNAME.self)
let viewFromXib = bundle.loadNibNamed("XIB_FILE_NAME", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
addSubview(viewFromXib)

Example

let bundle = Bundle(for: MTPale.self)
let viewFromXib = bundle.loadNibNamed("MTPale", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
addSubview(viewFromXib)
Sameer Nawaz
  • 101
  • 10