Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 437 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading a XIB file to a UIView Swift

#11
In my project I implemented the following (very similar to Peter's Solution)

import UIKit

// MARK: - Protocol Declaration

public protocol InterfaceBuilderInstantiable
{
/// The UINib that contains the view
///
/// Defaults to the swift class name if not implemented
static var associatedNib : UINib { get }
}

// MARK: - Default Implementation

extension InterfaceBuilderInstantiable
{
/// Creates a new instance from the associated Xib
///
/// - Returns: A new instance of this object loaded from xib
static func instantiateFromInterfaceBuilder() -> Self
{
return associatedNib.instantiate(withOwner:nil, options: nil)[0] as! Self
}

static var associatedNib : UINib
{
let name = String(describing: self)
return UINib(nibName: name, bundle: Bundle.main)
}
}

To use, you just simply implement the protocol:

class MyView: UIView, InterfaceBuilderInstantiable
{
// The rest

And if your nib is the same name as your class (`MyView.xib`), you're set: the default implementation of the protocol looks for a nib with the same name as the class in the main bundle.

Of course, if your nib is in another bundle or has a different name you can override the `associatedNib` and return your own nib.
Reply

#12
for **swift 3**

class YourClass: UIView {
class func instanceFromNib() -> YourClass {
return UINib(nibName: "YourClassNibName", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! YourClass
}
}
Reply

#13
Using Swift 3.0

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

#14
let xibView = NSBundle.mainBundle().loadNibNamed("NameXibView", owner: nil, options: nil)[0] as! UIView
Reply

#15
Usually I use the following way to load a xib file owned by a custom `UIView`:

NSBundle.mainBundle().loadNibNamed(nibName, owner: self, options: nil)[0];
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through