Loading a XIB file to a UIView Swift

load xib in viewcontroller swift 4
load uiview from xib swift 4
add xib to viewcontroller swift
show xib as popup swift
how to initialize uiview in swift
how to register xib file in swift
loadnibnamed
add xib to storyboard

I am trying to load my XIB file into a UIView but I am having some trouble. I have the required override functions but they seem to be crashing. Saying this error, warning:

could not load any Objective-C class information. This will significantly reduce the quality of type information available.

I was wondering if someone could show me how to properly load the XIB file into a UIView

import UIKit

class Widget: UIView {

    let view = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)

        //call function

        loadNib()

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        loadNib()

        //fatalError("init(coder:) has not been implemented")
    }

    func loadNib() {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "nib", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        self.addSubview(view);  
    }
}

I uses this in one of our projects, maby its usefull to you

import UIKit

class RegisterPageView: UIView {

        class func instanceFromNib() -> RegisterPageView {
            return UINib(nibName: "RegisterPageView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! RegisterPageView
        }
}

Load a UIView from nib in Swift, Update - using Swift 3 syntax​​ This is a generic method that returns an optional object of type UIView. If it fails to load the view, it returns nil. Attempting to load a XIB file with the same name as the current class instance. If that fails, nil is returned. I have some strange problem with loading xib file in swift project. It's so frustrating because I already know how to do it in Obj-C. But since swift is swift so you can't do it like you did.. :/ So I have create IconTextFiled.xib and IconTextField.swift.

Using Swift 3.0

let viewFromNib: UIView? = Bundle.main.loadNibNamed("NibName", 
    owner: nil, 
    options: nil)?.first

Custom UIView subclass from a xib file, Do you want to learn how to load a xib file to create a custom view object? Well, this UIKit tutorial is just for you written in Swift. I'm being a bit stubborn about using a XIB file to layout the custom view, as it has worked for well me in the past in Obj-C, so before I throw in the towel and do it purely through code, maybe you have some ideas about this behavior: I'm able to get my XIB-designed custom subview to show up in the view controller, however the outlets are nil

Improved DevAndArtist UIView extension

public extension UIView
{
    static func loadFromXib<T>(withOwner: Any? = nil, options: [UINib.OptionsKey : Any]? = nil) -> T where T: UIView
    {
        let bundle = Bundle(for: self)
        let nib = UINib(nibName: "\(self)", bundle: bundle)

        guard let view = nib.instantiate(withOwner: withOwner, options: options).first as? T else {
            fatalError("Could not load view from nib file.")
        }
        return view
    }
}

Usage

let view = CustomView.loadFromXib()
let view = CustomView.loadFromXib(withOwner: self)
let view = CustomView.loadFromXib(withOwner: self, options: [UINibExternalObjects: objects])

External Objects discussion

Custom UIView with XIB file - Swift2Go, Custom UIView with XIB file is a very common practice in iOS import UIKit​extension UIView { /** Loads instance from nib with the same name. 4: Create a custom file UIView extended file (like CustomUIView.swift) Create the custom file UIView Same as .xib file but you want to select the option in source “Cocoa Touch Class” and create the new file with name CustomUIView.swift which have UIView subclass. 5: Set .xib File owner to custom class (It must be same as class name )

Here is my approach (written in Swift 3.1):

protocol XibDesignable : class {}

extension XibDesignable where Self : UIView {

    static func instantiateFromXib() -> Self {

        let dynamicMetatype = Self.self
        let bundle = Bundle(for: dynamicMetatype)
        let nib = UINib(nibName: "\(dynamicMetatype)", bundle: bundle)

        guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {

            fatalError("Could not load view from nib file.")
        }
        return view
    }
}

extension UIView : XibDesignable {}

Now I simply can create any UIView subclass from a Xib (assuming there is one) like so MyCustomView.instantiateFromXib(). Remember to name your Xib file exactly as your UIView subclass and set the type of the main view in that Xib file correctly.


As soon as SE-0068 will be implemented one could drop the protocol and move the function directly into the UIView extension.


Just a note: The original post uses a commonly used pattern with a nested view. IMHO this is a bad pattern which does not utilize the resources and only creates unnecessary view hierarchy.

Swift, Now that you've added your custom UIView as the file's owner, you should be able to tap the assistant editor button in the upper right, and load  I created a UIView outlet in SomeView.swift, linking it to the top level view in the XIB file (named it "view" for convenience). I then added other outlets to other controls in the XIB file as needed. In SomeView.swift, I loaded the XIB inside the init or init:frame: CGRect initializer. There is no need to assign anything to "self".

Swift 4.x

let myView = Bundle.main.loadNibNamed("yourXibView", owner: nil, options: nil)![0] as! UIView

Loading a View from a XIB, First, we create a class named CustomView , which is a subclass of UIView: Bildschirmfoto 2015-07-04 um 15.38.44. Then we create a XIB file. // NibLoadingView.swift import UIKit /* Usage: - Subclass your UIView from NibLoadView to automatically load an Xib with the same name as your class - Set the class name to File's Owner in the Xib file */ @IBDesignable class NibLoadingView: UIView { @IBOutlet weak var view: UIView!

How to load a xib file in a UIView, . Choose the View option under the user interface menu. Adding cells in a xib is not supported. If you must use a xib file, then you will need a separate xib which contains the UICollectionView cell. Storyboards may be a better solution. It is not clear what you are trying to achieve. UICollectionView has specific means for creating headers which uses the datasource and delegate.

Xcode: What is a XIB file?, Implement the init functions of the view. Do not Create a xib file and corresponding .swift file in our case its SomeDemoView. 2. Set File's  In today’s project, Xib is used for the project, and an animation effect is needed in the page. The uiview animation is shown as follows: To change the width of the view for animation, drag this constraint to the viewcontroller to become an attribute Use the following methods to find that animation cannot be performed: […]

Make A View Using .xib file Xcode 10 2019 Guide, Show the above view. Add the following code to the Pokemon.swift class: import UIKit class Pokemon: UIView { // MARK: - Initializers override init(  Then I want a different .xib file called firstView.xib and load it inside of containerView. So I can have a bunch of stuff on firstView.xib and a bunch of different stuff in rootView.xib and load my firstView.xib inside of containerView in rootView.xib but since it only takes up half of the screen you would still see the stuff on rootView.xib

Comments
  • Is your XIB file really named "nil"?
  • Seems to work fine...which specific line is it crashing?
  • It not works with thisView.alpha = 0 , thisView.superview
  • I like this approach. In my case, .xib files contain only one view of the current class and guard let view = nib.instantiate(withOwner: nil, options: nil).last crashed, since .last returned nil. For some reason, guard let view = nib.instantiate(withOwner: nil, options: nil)[0] works great though. Just change .last to [0]
  • There is no .last in my sample. Furthermore .first cannot be nil if the array really contains something. [0] will also crash if the array is empty.
  • Can you fill some more blanks in? Where does that go and where does it get called? I've been mucking around with this longer than I care to admit and everything I've found is either out of date Swift or Objective-C. Seems like it's a simple exercise, but I'm too ****ing dumb to figure it out.