Add (addsubview) custom/reusable view from XIB OS X App - swift

Here is the code for my custom view. I also have a custom XIB file with the view mainView in it. I need to use this 3 times in a stackView in a view controller. How do I do that?
class PlatformView: NSView {
#IBOutlet weak var mainView: NSView!
#IBOutlet weak var currentPriceLabel: NSTextField!
//Here is the button
#IBAction func testButtonPressed(_ sender: Any) {
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
NSNib(nibNamed: NSNib.Name(rawValue: "PlatformView"), bundle: nil)?.instantiate(withOwner: self, topLevelObjects: nil)
addSubview(mainView)
self.mainView.frame = self.bounds
}
}
Here is the code for my mainViewController
class ViewController: NSViewController {
#IBOutlet weak var platformStackView: NSStackView!
var platformView1:PlatformView!
var platformView2:PlatformView!
var platformView3:PlatformView!
override func viewDidLoad() {
super.viewDidLoad()
//This part is not working in a macOS app
if let tempView = Bundle.main.loadNibNamed(NSNib.Name(rawValue: "PlatformView"), owner: self, topLevelObjects: nil)?.first as? PlatformView {
self.platformView1 = tempView
self.platformView1.currentPriceLabel = "$1.35"
self.platformStackView.addArrangedSubview(self.platformView1)
self.platformStackView.addSubview()
self.platformStackView.addView(tempView, in: self)
//Which one of these three methods should I use?
}
}
}
I have buttons in the Platform View which I would like to connect via IBAction outlets. How would I handle those?
I also added three ways to add a view to stack view. Which one is best to use?
It also says that .loadNibNamed will return a Bool not a instance of a view. How do I load the view multiple times in this View controller? I obviously also need to make changes through the life of the VC so I need to keep the instance of that view.

First you should use:
self.platformStackView.addArrangedSubview(self.platformView1)
because this will add self.platformView1 as both a sub view (as per addSubView) and add it to the list of views the stack view arranges.
Second the XIB file can contain multiple top level objects so can't expect it to return a single object. The Bool return indicates if the XIB file as a whole was successfully loaded and the contents of the top level objects will be put in the topLevelObjects parameter which is an array.
So you do something like this:
var topLevelObjects: NSArray?
if Bundle.main.loadNibNamed(NSNib.Name(rawValue: "TestView"), owner: self, topLevelObjects: &topLevelObjects) {
// Use the objects as you need including searching for a specific one you may require.
}
As an alternative you can also do this:
if let nib = NSNib(nibNamed: NSNib.Name(rawValue: "TestView"), bundle: nil) {
if nib.instantiate(withOwner: self, topLevelObjects: &temp) {
// Use the objects as you need including searching for a specific one you may require.
}
}
When used this is the result:
Here is a worked example of loading a custom class from a XIB file and adding it to the current view controllers view three times. The button action will load the XIB file instantiate three copies of it and add the TestView class it finds to the view controllers main view (at 0, 110 & 220):
#IBAction func buttonAction(_ sender: Any) {
var objectArray: NSArray?
if let nib = NSNib(nibNamed: NSNib.Name(rawValue: "TestView"), bundle: nil) {
if nib.instantiate(withOwner: self, topLevelObjects: &objectArray),
let topLevelObjects = objectArray {
for object in topLevelObjects {
if let testView = object as? TestView {
self.view.addSubview(testView)
testView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
testView.testLabel.stringValue = "Test Label 1"
}
}
}
if nib.instantiate(withOwner: self, topLevelObjects: &objectArray),
let topLevelObjects = objectArray {
for object in topLevelObjects {
if let testView = object as? TestView {
self.view.addSubview(testView)
testView.frame = CGRect(x: 110, y: 0, width: 100, height: 100)
testView.testLabel.stringValue = "Test Label 2"
}
}
}
if nib.instantiate(withOwner: self, topLevelObjects: &objectArray),
let topLevelObjects = objectArray {
for object in topLevelObjects {
if let testView = object as? TestView {
self.view.addSubview(testView)
testView.frame = CGRect(x: 220, y: 0, width: 100, height: 100)
testView.testLabel.stringValue = "Test Label 3"
}
}
}
}
}
The TestView code looks like this:
class TestView: NSView {
#IBOutlet var testLabel: NSTextField!
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
}
override func awakeFromNib() {
super.awakeFromNib()
self.testLabel.stringValue = "Init"
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
Nothing too complex with that custom view. It has a single NSTextField which is all setup correctly and so can just have it's string value set.
That all works for me so should be the way to go (this is the first MacOS app I have actually done as I am normally an iOS guy).

Related

How to declare a variable type depending on a ternary expression?

I have this enum :
enum WeatherMapDisplayType {
case temperature
case wind
}
Can I declare a variable like this ?
let markerView: mode == .temperature ? WeatherMapItemTemp : WeatherMapItemWind = UIView.fromNib()
Knowing that mode is of type WeatherMapDisplayType
How can I handle this scenario in an elegant way ?
EDIT:
I want to be able to do something like this :
let markerView: WeatherMapItem = UIView.fromNib()
markerView.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
markerView.setupWeatherInformations(mode: self.currentDisplayMode, forecast: forecasts)
marker.iconView = markerView
Previously I only had WeatherMapItem type.
Then I have been asked to add an other weather map item, that is why I have the WeatherMapItemTemp and WeatherMapItemWind now (also corresponding to my enum display type).
func setupWeatherInformations(forecast: Forecast.HourlyForecast)
This is a method in my custom classes in order to configure the outlets.
But I don't have access to this method if I init my custom view from frame, because it's of UIView type.
Add a common protocol to these views:
protocol WeatherMapItem where Self: UIView {
func setupWeatherInformations()
}
class WeatherMapItemTemp: UIView, WeatherMapItem {
func setupWeatherInformations() {
// setup
}
}
class WeatherMapItemWind: UIView, WeatherMapItem {
func setupWeatherInformations() {
// setup
}
}
Add a computed variable to the enum:
enum WeatherMapDisplayType {
case temperature
case wind
var view: (UIView & WeatherMapItem)? {
var nibName: String? = nil
switch self {
case .temperature:
nibName = "WeatherMapItemTemp"
case .wind:
nibName = "WeatherMapItemWind"
}
if let nibName = nibName, let views = Bundle.main.loadNibNamed(nibName, owner: nil), views.count > 0 {
return views[0] as? UIView & WeatherMapItem
}
return nil
}
}
Now you can generate a view like so:
// assuming mode is the variable of type WeatherMapDisplayType
let view = mode.view
view?.setupWeatherInformations()
return view
Updated Answer
If you want to set data in that single ternary statement then declare one function in your custom view class.
Something like following
class WeatherMapItemTemp: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public func setForecast(forecastData: Forecast) -> WeatherMapItemTemp {
let view = WeatherMapItemTemp.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
//Set your appropriate value to your view objects
return view
}
}
Use this as following
let markerView = mode == .temperature ? (WeatherMapItemTemp().setForecast(forecastData: YOUR_FORECAST_OBJ)) : (WeatherMapItemWind().setForecast(forecastData: YOUR_FORECAST_OBJ))
Update 2
Yes you will need to load that from a nib
Update setForecast as following.
class func setForecast(forecastData: Forecast) -> WeatherMapItemTemp {
let view = UINib(nibName: "WeatherMapItemTemp", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! WeatherMapItemTemp
//Do your further stuff here
return view
}

iCarousel shows images only after resize

I have imported the iCarousel provided by nicklockwood into a macOs app.
https://github.com/nicklockwood/iCarousel
The app is written in Swift.
After managing to get everything working (importing .m and .h files, adding Bridging Header) there is one minor thing.
Once the app is started and the NSViewController is activated I see a blank ViewController. Only if I start resizing the view I see the iCarousel with all loaded picture.
My code looks like the following:
class CarouselViewController: NSViewController, iCarouselDataSource, iCarouselDelegate {
var images = [NSImage]()
#IBOutlet var carousel: iCarousel!
override func awakeFromNib() {
super.awakeFromNib()
loadImages()
}
override func viewDidLoad() {
super.viewDidLoad()
self.carousel.type = iCarouselType.coverFlow
self.carousel.dataSource = self
self.carousel.delegate = self
carousel.reloadData()
}
func loadImages() {
let filePath = "/pics/"
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.picturesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
let path = paths?.appending(filePath)
//
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path!)!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("jpg") || element.hasSuffix("png") {
if let image = NSImage(contentsOfFile: path! + element) {
print("File: \(path!)\(element)")
self.images.append(image)
}
}
}
}
func numberOfItems(in carousel: iCarousel) -> Int {
return images.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: NSView?) -> NSView {
let imageView = NSImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
imageView.image = self.images[index]
imageView.imageScaling = .scaleAxesIndependently
return imageView
}
func carouselItemWidth(_ carousel: iCarousel) -> CGFloat {
return 200.0
}
}
How can I manage to display my iCarousel without resizing first?
Thank you
I guess I've found one solution:
The drawing of the actual images in the carousel is triggered by layOutItemViews or layoutSubviews. One function that is public accessible is setType which allows to set the carousel type.
If I set the type after assigning dataSource and after loading the data, the images will be displayed fine.
So the most simple answer is to change viewDidLayout like the following:
override func viewDidLoad() {
super.viewDidLoad()
self.carousel.dataSource = self
self.carousel.delegate = self
carousel.reloadData()
// Triggers layOutItemView and thus will render all images.
self.carousel.type = iCarouselType.coverFlow
}
Now it is also possible to assign datasource via storyboard and the Connection inspector.

Passing data from view controller to view controller with a delegate

Tried to send data from one view controller (from an alamofire request) to the next view controller in a navigation controller.
I tried to this with a delegate, but I do not get it working. I allready know this is not the way, but i need to find a solution to get it working.
See below for the code, from view controller that sends variabels:
protocol SendDataToScanInfo {
func sendData (vendorname01 : String, productname01: String, productstatus01: String, productdescription01: String)
}
class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate, CLLocationManagerDelegate{
var delegate:SendDataToScanInfo?
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
Alamofire.request(URL_SCAN_ID, method: .post, parameters: ScanParameters, encoding: JSONEncoding.default) .responseJSON
{
response in
//printing response
print(response.request!)
print(response.response!)
print(response.data!)
print(response.result)
print(response.error)
//getting the json value from the server
let value = response.result.value
print(value!)
let json = JSON(value!)
let productdesc0:JSON = json["productdesc"]
let productdescString = productdesc0.string
let productname0:JSON = json["productname"]
let productnameString = productname0.string
let tagstate0:JSON = json["tagstate"]
let tagstateString = tagstate0.string
let vendorname0:JSON = json["vendorname"]
let vendornameString = vendorname0.string
//self.performSegue(withIdentifier: "ScanInfo", sender: productdescString)
self.delegate?.sendData(vendorname01: vendornameString!, productname01: productnameString!, productstatus01: tagstateString!, productdescription01: productdescString!)
print(vendornameString)
}
if code != nil
{
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destination = mainStoryboard.instantiateViewController(withIdentifier: "ScanInfo")
navigationController?.pushViewController(destination, animated: true)
}
captureSession.stopRunning();
//self.dismiss(animated: true, completion: nil)
}
}
}
Next Viewcontroller should receive it:
class ScanInfoViewController: UIViewController, SendDataToScanInfo {
#IBOutlet weak var Vendor: UILabel!
#IBOutlet weak var VendorScan: UILabel!
#IBOutlet weak var Product: UILabel!
#IBOutlet weak var ProductScan: UILabel!
#IBOutlet weak var Status: UILabel!
#IBOutlet weak var DescriptionScan: UILabel!
#IBOutlet weak var Description: UILabel!
#IBOutlet weak var StatusScan: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
DescriptionScan.text = descriptionBLA
print("jddjd", descriptionBLA)
let URL_SCAN_INFO = "http://makeitrain.get-legit.com:8998/checktag"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sendData(vendorname01: String, productname01: String, productstatus01: String, productdescription01: String) {
VendorScan.text = vendorname01
ProductScan.text = productname01
DescriptionScan.text = productdescription01
StatusScan.text = productstatus01
print("MMMM", StatusScan.text)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ScanInfo" {
let sendingVC: ScanController = segue.destination as! ScanController
sendingVC.delegate = self
}
}
}
I hope some one can help me!
To pass data forward, like williej926 said, segues are the way to go. To pass data forward from one viewcontroller to another, you need to create a segue between these two viewcontrollers, and give the segue an identifier if there is more than one segue in your project that you are using to pass data, then this is a must. In your first view controller's class you should create a prepareForSegue method by using the one built-in. In that prepareForSegue method, you write if the segue's identifier is equal to the one that you have set in your storyboard. In that if statement, you need to tell this viewcontroller what your segue's destination is. To do that write let destination = segue.destination as! nextViewControllerClass. To access variables and set them in your second viewcontroller, write destination.variableName = thisVariableName. Here is an example showing you what this looks like purely in code.
In First View Controller's class
class FirstViewController: UIViewController {
var thisString: String?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
if(identifier == "secondViewController") {
let destination = segue.destination as! SecondViewController//SecondViewController is second view controller's class
destination.myString = thisString
}
}
}
}
Second View Controller's Class
class SecondViewController: UIViewController {
var myString: String?//this will equal to thisString in FirstViewController
}
I wrote an answer about this not too long ago :
One the simpler way to pass info from one VC to another is either through an initiliazer, or through a variable that you set before presenting the second VC.
The secone method would have you go through a delegate, mainly when passing data BACK to the initial VC. Either way, you'd need a setup similar to this:
class LoggedInVCViewController : UIViewController {
var info : String? {
didSet {
if let newInfo = self.info {
//do what ever you need to do here
}
}
}
override viewDidLoad() {
super.viewDidLoad()
}
}
func presentLoggedInScreen(yourInfo: String) {
let stroyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loggedInVC:LoggedInVCViewController =
storyboard.instantiateViewController(withIdentifier: "loggedInVC") as!
LoggedInVCViewController
loggedInVC.info = yourInfo
self.present(loggedInVC, animated: true, completion: nil)
}
class LoggedInVCViewController : UIViewController {
var info : Any? {
get {
if let this = self.info {
return this
} else {
return nil
}
} set {
if let new = newValue {
//
}
}
}
init(info: Any?) {
//This first line is key, it also calls viewDidLoad() internally, so don't re-write viewDidLoad() here!!
super.init(nibName: nil, bundle: nil)
if let newInfo = info {
//here we check info for whatever you pass to it
self.info = newInfo
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Which is then used :
func presentLoggedInScreen(yourInfo: String) {
let loggedInVC = LoggedInVCViewController(info: yourInfo)
self.present(loggedInVC, animated: true, completion: nil)
}
Or if you're using the variable approach:
func presentLoggedInScreen(yourInfo: String) {
let loggedInVC = LoggedInVCViewController()
loggedInVC.info = yourInfo
self.present(loggedInVC, animated: true, completion: nil)
}
I also go over, and link to other post which talk about the caveats of using Storyboards, and custom initializers to pass on data. I'd read over them as well!
The best way to do this is by using a segue. Connect a segue between the controllers and in the prepareForSegue you add a variable that represents the controller you are segueing to like so: let viewController = segue.destination as! viewController. Now you can access and change variables inside viewController using viewController.variable.

How to pass value from NSViewController to custom NSView of NSPopover?

By using the delegation protocol I have tried to pass a string (inputFromUser.string) from NSViewController - mainController to custom subclass of NSView of NSPopover - PlasmidMapView, to drawRect function, see code below. But, it didn’t work. I don’t know where a mistake is. Maybe there is another way to pass this string.
Update
File 1.
protocol PlasmidMapDelegate {
func giveDataForPLasmidMap(dna: String)
}
class MainController: NSViewController {
#IBOutlet var inputFromUser: NSTextView!
var delegate: plasmidMapDelegate?
#IBAction func actionPopoverPlasmidMap(sender: AnyObject) {
popoverPlasmidMap.showRelativeToRect(sender.bounds,
ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
let dna = inputDnaFromUser.string
delegate?.giveDataForPLasmidMap(dna!)
}
}
File 2
class PlasmidMapView: NSView, PlasmidMapDelegate {
var dnaForMap = String()
func giveDataForPLasmidMap(dna: String) {
dnaForMap = dna
}
override func drawRect(dirtyRect: NSRect) {
let objectOfMainController = MainController()
objectOfMainController.delegate = self
//here I have checked if the string dnaForMap is passed
let lengthOfString = CGFloat(dnaForMap.characters.count / 10)
let pathRect = NSInsetRect(self.bounds, 10, 45)
let path = NSBezierPath(roundedRect: pathRect,
xRadius: 5, yRadius: 5)
path.lineWidth = lengthOfString //the thickness of the line should vary in dependence on the number of typed letter in the NSTextView window - inputDnaFromUser
NSColor.lightGrayColor().setStroke()
path.stroke()
}
}
Ok, there's some architecture mistakes. You don't need delegate method and protocol at all. All you just need is well defined setter method:
I. Place your PlasmidMapView into NSViewController-subclass. This view controller must be set as contentViewController-property of your NSPopover-control. Don't forget to set it the way you need in viewDidLoad-method or another.
class PlasmidMapController : NSViewController {
weak var mapView: PlacmidMapView!
}
II. In your PlacmidMapView don't forget to call needsDisplay-method on dna did set:
class PlasmidMapView: NSView {
//...
var dnaForMap = String() {
didSet {
needsDisplay()
}
//...
}
III. Set dna-string whenever you need from your MainController-class.
#IBAction func actionPopoverPlasmidMap(sender: AnyObject) {
popoverPlasmidMap.showRelativeToRect(sender.bounds,
ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
let dna = inputDnaFromUser.string
if let controller = popoverPlasmidMap.contentViewController as? PlasmidMapController {
controller.mapView.dna = dna
} else {
fatalError("Invalid popover content view controller")
}
}
In order to use delegation your class PlasmidMapView needs to have an instance of the MainController (btw name convention is Class, not class) and conform to the PlasmidMapDelegate (once again name convention dictates that it should be PlasmidMapDelegate). With that instance you then can:
mainController.delegate = self
So, after several days I have found a solution without any protocols and delegation as Astoria has mentioned. All what I needed to do was to make #IBOutlet var plasmidMapIBOutlet: PlasmidMapView!for my custom NSView in MainController class and then to use it to set the value for the dnaForMap in #IBAction func actionPopoverPlasmidMap(sender: AnyObject).
class PlasmidMapView: NSView
{
var dnaForMap = String()
}
class MainController: NSViewController
{
#IBOutlet var inputFromUser: NSTextView!
#IBOutlet var plasmidMapIBOutlet: PlasmidMapView!
#IBAction func actionPopoverPlasmidMap(sender: AnyObject)
{
plasmidMapIBOutlet.dnaForMap = inputDnaFromUser.string!
popoverPlasmidMap.showRelativeToRect(sender.bounds,
ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
}
}

Load a UIView from nib in Swift

Here is my Objective-C code which I'm using to load a nib for my customised UIView:
-(id)init{
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"myXib" owner:self options:nil];
return [subviewArray objectAtIndex:0];
}
What is the equivalent code in Swift?
My contribution:
extension UIView {
class func fromNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}
Then call it like this:
let myCustomView: CustomView = UIView.fromNib()
..or even:
let myCustomView: CustomView = .fromNib()
Original Solution
I created a XIB and a class named SomeView (used the same name for
convenience and readability). I based both on a UIView.
In the XIB, I changed the "File's Owner" class to SomeView (in the identity inspector).
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 with code" initializer. There is no need to assign anything to "self". As soon as the XIB is loaded, all outlets are connected, including the top level view. The only thing missing, is to add the top view to the view hierarchy:
.
class SomeView: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("SomeView", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
...
}
Note that this way I get a class that loads itself from nib. I could then use SomeView as a class whenever UIView could be used in the project (in interface builder or programmatically).
Update - using Swift 3 syntax
Loading a xib in the following extension is written as an instance method, which can then be used by an initializer like the one above:
extension UIView {
#discardableResult // 1
func fromNib<T : UIView>() -> T? { // 2
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as? T else { // 3
// xib not loaded, or its top view is of the wrong type
return nil
}
self.addSubview(contentView) // 4
contentView.translatesAutoresizingMaskIntoConstraints = false // 5
contentView.layoutAttachAll(to: self) // 6
return contentView // 7
}
}
Using a discardable return value since the returned view is mostly of no interest to caller when all outlets are already connected.
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.
Adding the top level view to the view hierarchy.
This line assumes we're using constraints to layout the view.
This method adds top, bottom, leading & trailing constraints - attaching the view to "self" on all sides (See: https://stackoverflow.com/a/46279424/2274829 for details)
Returning the top level view
And the caller method might look like this:
final class SomeView: UIView { // 1.
required init?(coder aDecoder: NSCoder) { // 2 - storyboard initializer
super.init(coder: aDecoder)
fromNib() // 5.
}
init() { // 3 - programmatic initializer
super.init(frame: CGRect.zero) // 4.
fromNib() // 6.
}
// other methods ...
}
SomeClass is a UIView subclass that loads its content from a SomeClass.xib file. The "final" keyword is optional.
An initializer for when the view is used in a storyboard (remember to use SomeClass as the custom class of your storyboard view).
An initializer for when the view is created programmatically (i.e.: "let myView = SomeView()").
Using an all-zeros frame since this view is laid out using auto-layout.
Note that an "init(frame: CGRect) {..}" method is not created independently, since auto-layout is used exclusively in our project.
& 6. Loading the xib file using the extension.
Credit: Using a generic extension in this solution was inspired by Robert's answer below.
Edit
Changing "view" to "contentView" to avoid confusion. Also changed the array subscript to ".first".
Now being able to return -> Self in swift helps simplify this a bit. Last confirmed on Swift 5.
extension UIView {
class func fromNib(named: String? = nil) -> Self {
let name = named ?? "\(Self.self)"
guard
let nib = Bundle.main.loadNibNamed(name, owner: nil, options: nil)
else { fatalError("missing expected nib named: \(name)") }
guard
/// we're using `first` here because compact map chokes compiler on
/// optimized release, so you can't use two views in one nib if you wanted to
/// and are now looking at this
let view = nib.first as? Self
else { fatalError("view of type \(Self.self) not found in \(nib)") }
return view
}
}
If your .xib file and subclass share the same name, you can use:
let view = CustomView.fromNib()
If you have a custom name, use:
let view = CustomView.fromNib(named: "special-case")
NOTE:
If you're getting the error "view of type YourType not found in.." then you haven't set the view's class in the .xib file
Select your view in the .xib file, and press cmd + opt + 4 and in the class input, enter your class
Swift 4 - 5.1 Protocol Extensions
public protocol NibInstantiatable {
static func nibName() -> String
}
extension NibInstantiatable {
static func nibName() -> String {
return String(describing: self)
}
}
extension NibInstantiatable where Self: UIView {
static func fromNib() -> Self {
let bundle = Bundle(for: self)
let nib = bundle.loadNibNamed(nibName(), owner: self, options: nil)
return nib!.first as! Self
}
}
Adoption
class MyView: UIView, NibInstantiatable {
}
This implementation assumes that the Nib has the same name as the UIView class. Ex. MyView.xib. You can modify this behavior by implementing nibName() in MyView to return a different name than the default protocol extension implementation.
In the xib the files owner is MyView and the root view class is MyView.
Usage
let view = MyView.fromNib()
try following code.
var uiview :UIView?
self.uiview = NSBundle.mainBundle().loadNibNamed("myXib", owner: self, options: nil)[0] as? UIView
Edit:
import UIKit
class TestObject: NSObject {
var uiview:UIView?
init() {
super.init()
self.uiview = NSBundle.mainBundle().loadNibNamed("myXib", owner: self, options: nil)[0] as? UIView
}
}
If you have a lot of custom views in your project you can create class like UIViewFromNib
Swift 2.3
class UIViewFromNib: UIView {
var contentView: UIView!
var nibName: String {
return String(self.dynamicType)
}
//MARK:
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}
//MARK:
private func loadViewFromNib() {
contentView = NSBundle.mainBundle().loadNibNamed(nibName, owner: self, options: nil)[0] as! UIView
contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
contentView.frame = bounds
addSubview(contentView)
}
}
Swift 5
class UIViewFromNib: UIView {
var contentView: UIView!
var nibName: String {
return String(describing: type(of: self))
}
//MARK:
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}
//MARK:
func loadViewFromNib() {
let bundle = Bundle(for: UIViewFromNib.self)
contentView = UINib(nibName: nibName, bundle: bundle).instantiate(withOwner: self).first as? UIView
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.frame = bounds
addSubview(contentView)
}
}
And in every class just inherit from UIViewFromNib, also you can override nibName property if .xib file has different name:
class MyCustomClass: UIViewFromNib {
}
I achieved this with Swift by the following code:
class Dialog: UIView {
#IBOutlet var view:UIView!
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = UIScreen.mainScreen().bounds
NSBundle.mainBundle().loadNibNamed("Dialog", owner: self, options: nil)
self.view.frame = UIScreen.mainScreen().bounds
self.addSubview(self.view)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Don't forget to connect your XIB view outlet to view outlet defined in swift. You can also set First Responder to your custom class name to start connecting any additional outlets.
Hope this helps!
Tested in Xcode 7 beta 4 , Swift 2.0 and iOS9 SDK .
The following code will assign xib to the uiview.
You can able to use this custom xib view in storyboard and access the IBOutlet object also.
import UIKit
#IBDesignable class SimpleCustomView:UIView
{
var view:UIView!;
#IBOutlet weak var lblTitle: UILabel!
#IBInspectable var lblTitleText : String?
{
get{
return lblTitle.text;
}
set(lblTitleText)
{
lblTitle.text = lblTitleText!;
}
}
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "SimpleCustomView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
}
}
Access customview programatically
self.customView = SimpleCustomView(frame: CGRectMake(100, 100, 200, 200))
self.view.addSubview(self.customView!);
Source code - https://github.com/karthikprabhuA/CustomXIBSwift
Building on the above solutions.
This will work across all project bundles and no need for generics when calling fromNib().
Swift 2
extension UIView {
public class func fromNib() -> Self {
return fromNib(nil)
}
public class func fromNib(nibName: String?) -> Self {
func fromNibHelper<T where T : UIView>(nibName: String?) -> T {
let bundle = NSBundle(forClass: T.self)
let name = nibName ?? String(T.self)
return bundle.loadNibNamed(name, owner: nil, options: nil)?.first as? T ?? T()
}
return fromNibHelper(nibName)
}
}
Swift 3
extension UIView {
public class func fromNib() -> Self {
return fromNib(nibName: nil)
}
public class func fromNib(nibName: String?) -> Self {
func fromNibHelper<T>(nibName: String?) -> T where T : UIView {
let bundle = Bundle(for: T.self)
let name = nibName ?? String(describing: T.self)
return bundle.loadNibNamed(name, owner: nil, options: nil)?.first as? T ?? T()
}
return fromNibHelper(nibName: nibName)
}
}
Can be used like this:
let someView = SomeView.fromNib()
Or like this:
let someView = SomeView.fromNib("SomeOtherNibFileName")
Swift 4
Don't forget to write ".first as? CustomView".
if let customView = Bundle.main.loadNibNamed("myXib", owner: self, options: nil)?.first as? CustomView {
self.view.addSubview(customView)
}
If you want to use anywhere
The Best Solution is Robert Gummesson's answer.
extension UIView {
class func fromNib<T: UIView>() -> T {
return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}
Then call it like this:
let myCustomView: CustomView = UIView.fromNib()
I prefer this solution (based on the answer if #GK100):
I created a XIB and a class named SomeView (used the same name for convenience and readability). I based both on a UIView.
In the XIB, I changed the "File's Owner" class to SomeView (in the identity inspector).
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". As soon as the XIB is loaded, all outlets are connected, including the top level view. The only thing missing, is to add the top view to the view hierarchy:
class SomeView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
NSBundle.mainBundle().loadNibNamed("SomeObject", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("SomeObject", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
...
}
A nice way to do this with Swift is to use an enum.
enum Views: String {
case view1 = "View1" // Change View1 to be the name of your nib
case view2 = "View2" // Change View2 to be the name of another nib
func getView() -> UIView? {
return NSBundle.mainBundle().loadNibNamed(self.rawValue, owner: nil, options: nil).first as? UIView
}
}
Then in your code you can simply use:
let view = Views.view1.getView()
Updated for Swift 5
Somewhere define below:
extension UIView {
public class func fromNib<T: UIView>() -> T {
let name = String(describing: Self.self);
guard let nib = Bundle(for: Self.self).loadNibNamed(
name, owner: nil, options: nil)
else {
fatalError("Missing nib-file named: \(name)")
}
return nib.first as! T
}
}
And use above like:
let view: MyCustomView = .fromNib();
Which will search in same bundle as MyCustomView, then load MyCustomView.nib file (if file exists, and is added to project).
let subviewArray = NSBundle.mainBundle().loadNibNamed("myXib", owner: self, options: nil)
return subviewArray[0]
Swift 5 - Clean and easy to use extension
[Copy Paste from production project]
//
// Refactored by Essam Mohamed Fahmi.
//
import UIKit
extension UIView
{
static var nib: UINib
{
return UINib(nibName: "\(self)", bundle: nil)
}
static func instantiateFromNib() -> Self?
{
return nib.instantiate() as? Self
}
}
extension UINib
{
func instantiate() -> Any?
{
return instantiate(withOwner: nil, options: nil).first
}
}
Usage
let myCustomView: CustomView = .instantiateFromNib()
I just do this way :
if let myView = UINib.init(nibName: "MyView", bundle: nil).instantiate(withOwner: self)[0] as? MyView {
// Do something with myView
}
This sample uses the first view in the nib "MyView.xib" in the main bundle. But you can vary either the index, the nib name, or the bundle ( main by default ).
I used to awake views into the view init method or make generic methods as in the proposed answers above ( which are smart by the way ), but I don't do it anymore because I have noticed use cases are often different, and to cover all cases, generic methods become as complex as using the UINib.instantiate method.
I prefer to use a factory object, usually the ViewController that will use the view, or a dedicated factory object or view extension if the view needs to be used in multiple places.
In this example, a ViewController loads a view from nib.
The nib file can be changed to use different layouts for the same view class. ( This not nice code, it just illustrates the idea )
class MyViewController {
// Use "MyView-Compact" for compact version
var myViewNibFileName = "MyView-Standard"
lazy var myView: MyView = {
// Be sure the Nib is correct, or it will crash
// We don't want to continue with a wrong view anyway, so ! is ok
UINib.init(nibName: myViewNibFileName, bundle: nil).instantiate(withOwner: self)[0] as! MyView
}()
}
Swift 3 version of Logan's answer
extension UIView {
public class func fromNib(nibName: String? = nil) -> Self {
return fromNib(nibName: nibName, type: self)
}
public class func fromNib<T: UIView>(nibName: String? = nil, type: T.Type) -> T {
return fromNib(nibName: nibName, type: T.self)!
}
public class func fromNib<T: UIView>(nibName: String? = nil, type: T.Type) -> T? {
var view: T?
let name: String
if let nibName = nibName {
name = nibName
} else {
name = self.nibName
}
if let nibViews = Bundle.main.loadNibNamed(name, owner: nil, options: nil) {
for nibView in nibViews {
if let tog = nibView as? T {
view = tog
}
}
}
return view
}
public class var nibName: String {
return "\(self)".components(separatedBy: ".").first ?? ""
}
public class var nib: UINib? {
if let _ = Bundle.main.path(forResource: nibName, ofType: "nib") {
return UINib(nibName: nibName, bundle: nil)
} else {
return nil
}
}
}
Here is a clean and declarative way of programmatically loading a view using a protocol and protocol extension (Swift 4.2):
protocol XibLoadable {
associatedtype CustomViewType
static func loadFromXib() -> CustomViewType
}
extension XibLoadable where Self: UIView {
static func loadFromXib() -> Self {
let nib = UINib(nibName: "\(self)", bundle: Bundle(for: self))
guard let customView = nib.instantiate(withOwner: self, options: nil).first as? Self else {
// your app should crash if the xib doesn't exist
preconditionFailure("Couldn't load xib for view: \(self)")
}
return customView
}
}
And you can use this like so:
// don't forget you need a xib file too
final class MyView: UIView, XibLoadable { ... }
// and when you want to use it
let viewInstance = MyView.loadFromXib()
Some additional considerations:
Make sure your custom view's xib file has the view's Custom Class set (and outlets/actions set from there), not the File Owner's.
You can use this protocol/extension external to your custom view or internal. You may want to use it internally if you have some other setup work when initializing your view.
Your custom view class and xib file need to have the same name.
All you have to do is call init method in your UIView class.
Do it that way:
class className: UIView {
#IBOutlet var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func setup() {
UINib(nibName: "nib", bundle: nil).instantiateWithOwner(self, options: nil)
addSubview(view)
view.frame = self.bounds
}
}
Now, if you want to add this view as a sub view in view controller, do it that way in view controller.swift file:
self.view.addSubview(className())
Similar to some of the answers above but a more consistent Swift3 UIView extension:
extension UIView {
class func fromNib<A: UIView> (nibName name: String, bundle: Bundle? = nil) -> A? {
let bundle = bundle ?? Bundle.main
let nibViews = bundle.loadNibNamed(name, owner: self, options: nil)
return nibViews?.first as? A
}
class func fromNib<T: UIView>() -> T? {
return fromNib(nibName: String(describing: T.self), bundle: nil)
}
}
Which gives the convenience of being able to load the class from a self named nib but also from other nibs/bundles.
You can do this via storyboard, just add proper constraints for view. You can do this easily by subclassing any view from your own let's say BaseView:
Objective-C
BaseView.h
/*!
#class BaseView
#discussion Base View for getting view from xibFile
#availability ios7 and later
*/
#interface BaseView : UIView
#end
BaseView.m
#import "BaseView.h"
#implementation BaseView
#pragma mark - Public
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self prepareView];
}
return self;
}
#pragma mark - LifeCycle
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self prepareView];
}
return self;
}
#pragma mark - Private
- (void)prepareView
{
NSArray *nibsArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *view = [nibsArray firstObject];
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self addConstraintsForView:view];
}
#pragma mark - Add constraints
- (void)addConstraintsForView:(UIView *)view
{
[self addConstraints:#[[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0],
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0],
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0],
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]
]];
}
#end
Swift 4
import UIKit
class BaseView : UIView {
// MARK: - LifeCycle
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepareView()
}
override init(frame: CGRect) {
super.init(frame: frame)
prepareView()
}
internal class func xibName() -> String {
return String(describing: self)
}
// MARK: - Private
fileprivate func prepareView() {
let nameForXib = BaseView.xibName()
let nibs = Bundle.main.loadNibNamed(nameForXib, owner: self, options: nil)
if let view = nibs?.first as? UIView {
view.backgroundColor = UIColor.clear
view.translatesAutoresizingMaskIntoConstraints = false
addSubviewWithConstraints(view, offset: false)
}
}
}
UIView+Subview
public extension UIView {
// MARK: - UIView+Extensions
public func addSubviewWithConstraints(_ subview:UIView, offset:Bool = true) {
subview.translatesAutoresizingMaskIntoConstraints = false
let views = [
"subview" : subview
]
addSubview(subview)
var constraints = NSLayoutConstraint.constraints(withVisualFormat: offset ? "H:|-[subview]-|" : "H:|[subview]|", options: [.alignAllLeading, .alignAllTrailing], metrics: nil, views: views)
constraints.append(contentsOf: NSLayoutConstraint.constraints(withVisualFormat: offset ? "V:|-[subview]-|" : "V:|[subview]|", options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views))
NSLayoutConstraint.activate(constraints)
}
}
I provide 2 variants how to add constraints - common one and within visual format language - select any you want :)
Also, by default assumed that xib name has same name as implementation class name. If no - just change xibName parameter.
If you subclass your view from BaseView - you can easily put any view and specify class in IB.
If you want the Swift UIView subclass to be entirely self contained, and have the ability to be instantiated using init or init(frame:) without exposing the implementation detail of using a Nib, then you can use a protocol extension to achieve this. This solution avoids the nested UIView hierarchy as suggested by many of the other solutions.
public class CustomView: UIView {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var valueLabel: UILabel!
public convenience init() {
self.init(frame: CGRect.zero)
}
public override convenience init(frame: CGRect) {
self.init(internal: nil)
self.frame = frame
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
fileprivate func commonInit() {
}
}
fileprivate protocol _CustomView {
}
extension CustomView: _CustomView {
}
fileprivate extension _CustomView {
// Protocol extension initializer - has the ability to assign to self, unlike
// class initializers. Note that the name of this initializer can be anything
// you like, here we've called it init(internal:)
init(internal: Int?) {
self = Bundle.main.loadNibNamed("CustomView", owner:nil, options:nil)![0] as! Self;
}
}
class func loadFromNib<T: UIView>() -> T {
let nibName = String(describing: self)
return Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)![0] as! T
}
let nibs = Bundle.main.loadNibNamed("YourView", owner: nil, options: nil)
let shareView = nibs![0] as! ShareView
self.view.addSubview(shareView)
// Use this class as super view
import UIKit
class ViewWithXib: UIView {
func initUI() {}
private func xibSetup() {
let view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
initUI()
}
private func loadViewFromNib() -> UIView {
let thisName = String(describing: type(of: self))
let view = Bundle(for: self.classForCoder).loadNibNamed(thisName, owner: self, options: nil)?.first as! UIView
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
}
// Usages:
class HeaderView: ViewWithXib {
}
let header = HeaderView() // No need to load the view from nib, It will work
More powerful version based on Logan's answer
extension UIView {
public class func fromNib(nibName: String? = nil) -> Self {
return fromNib(nibName: nibName, type: self)
}
public class func fromNib<T: UIView>(nibName: String? = nil, type: T.Type) -> T {
return fromNib(nibName: nibName, type: T.self)!
}
public class func fromNib<T: UIView>(nibName: String? = nil, type: T.Type) -> T? {
var view: T?
let name: String
if let nibName = nibName {
name = nibName
} else {
name = self.nibName
}
if let nibViews = nibBundle.loadNibNamed(name, owner: nil, options: nil) {
if nibViews.indices.contains(nibIndex), let tog = nibViews[nibIndex] as? T {
view = tog
}
}
return view
}
public class var nibName: String {
return "\(self)".components(separatedBy: ".").first ?? ""
}
public class var nibIndex: Int {
return 0
}
public class var nibBundle: Bundle {
return Bundle.main
}
}
And you can use like
class BaseView: UIView {
override class var nibName: String { return "BaseView" }
weak var delegate: StandardStateViewDelegate?
}
class ChildView: BaseView {
override class var nibIndex: Int { return 1 }
}
The most convenient implementation. Here you need two methods, in order to return directly to the object of your class, not UIView.
viewId marked as a class, allowing override
Your .xib can contain more than one view of the top level, this situation is also
handled correctly.
extension UIView {
class var viewId: String {
return String(describing: self)
}
static func instance(from bundle: Bundle? = nil, nibName: String? = nil,
owner: Any? = nil, options: [AnyHashable : Any]? = nil) -> Self? {
return instancePrivate(from: bundle ?? Bundle.main,
nibName: nibName ?? viewId,
owner: owner,
options: options)
}
private static func instancePrivate<T: UIView>(from bundle: Bundle, nibName: String,
owner: Any?, options: [AnyHashable : Any]?) -> T? {
guard
let views = bundle.loadNibNamed(nibName, owner: owner, options: options),
let view = views.first(where: { $0 is T }) as? T else { return nil }
return view
}
}
Example:
guard let customView = CustomView.instance() else { return }
//Here customView has CustomView class type, not UIView.
print(customView is CustomView) // true
let bundle = Bundle(for: type(of: self))
let views = bundle.loadNibNamed("template", owner: self, options: nil)
self.view.addSubview(views?[0] as! UIView)
I prefer the below extension
extension UIView {
class var instanceFromNib: Self {
return Bundle(for: Self.self)
.loadNibNamed(String(describing: Self.self), owner: nil, options: nil)?.first as! Self
}
}
The difference between this and the top answered extension is you don't need to store it an constant or variable.
class TitleView: UIView { }
extension UIView {
class var instanceFromNib: Self {
return Bundle(for: Self.self)
.loadNibNamed(String(describing: Self.self), owner: nil, options: nil)?.first as! Self
}
}
self.navigationItem.titleView = TitleView.instanceFromNib
Robert Gummesson's Answer is perfect. But when we try to use it in SPM or framework it is not working.
I've modified like below to make it work.
internal class func fromNib<T: UIView>() -> T {
return Bundle.module.loadNibNamed(String(describing: T.self), owner: self, options: nil)![0] as! T
}