Giter Club home page Giter Club logo

pzcustomview's Introduction

PZCustomView

Deprecated

Latest versions of Xcode doesn't support custom file/project templates any more. Rest in peace ⚰️.

What is it ?

Xcode - custom UIView templates, that creates for you a nib - loadable UIView subclass with it's .xib file respectively.

It comes with Swift & Objective-C templates

Why even use it?

  1. Other methods are error prone and not consistent with all other components of the UIKit
  2. This approach will work just fine with both initializers:
init?(coder aDecoder: NSCoder)
init(frame: CGRect)

It means view user can initialize your custom view from code or just drag & drop it into any UI file (.storyboard or .xib).

  1. Because of how it is easy to setup this custom views you can compose them in any possible hierarchies without penalties by only using Xcode UI builder

Some insights

Created file (.swift) will have following structure:

class TestView: UIView {
    @IBOutlet weak private(set) var contentView: UIView!
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    func commonInit() {
        loadFromNib()
        
        contentView.frame = bounds
        
        addSubview(contentView)
    }
}

And a UIView extension if you opted in for it. You should do so only first time and it’s a good idea to move this extension to a separate file

extension UIView {
    func loadFromNib() {
        let nibName = String(describing: type(of: self))
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName, bundle: bundle)
        nib.instantiate(withOwner: self)
    }
}

Let FancyCustomView be the class name indicated at creation, then you will have following files created for you

FancyCustomView.swift
FancyCustomView.xib

So, for nibName is enough to have:

let nibName = String(describing: type(of: self))

In this case the nibName will have FancyCustomView value

Here we load class Bundle object:

let bundle = Bundle(for: type(of: self))

Initiating UINib file:

let nib = UINib(nibName: nibName, bundle: bundle)

But it actually only caches the contents of the nib file in memory, not doing actual work here; From Apple docs:

A UINib object caches the contents of a nib file in memory, ready for unarchiving and instantiation.

So, next we calling:

nib.instantiate(withOwner: self)

Only at this stage contentView and any other outlets get their values from .xib

And More

Because of how nib loading work in UIKit, nib - File’s owner should be an object that is already initialized and this pattern can be observed already in:

  1. UIViewController with view
  2. UITableViewCell / UICollectionViewCell with contentView

So, from outside we will initialize only a wrapper that will load from .xib it’s content in form of contentView and any subviews added to it;

By default contentView has autoresizingMask with autoresizing width & height and backgroundColor set to clear:

<autoresizingMask key=“autoresizingMask” widthSizable=“YES” heightSizable=“YES”/>
<color key=“backgroundColor” red=“0.0” green=“0.0” blue=“0.0” alpha=“0.0” colorSpace=“custom” customColorSpace=“sRGB”/>

That’s why this part is missing from commonInit.

Only customization needed to contentView observed in code is:

contentView.frame = bounds

After this point there is no need to worry about layout if constraints are set correctly inside your FancyCustomView.xib (remember UITableViewCell analogy, things here work in same way)

How to install

  1. Clone or download repository
  2. Copy Custom View.xctemplate folder to:

~/Library/Developer/Xcode/Templates/File Templates/User Interface/

How to use

  1. Access from Xcode menu New File option.
  2. In iOS tab, User interface category find Custom view option.

  1. After you complete wizard steps, you will have in project your custom view files ( .swift + .xib or .h, .m + .xib).

  1. Go to a storyboard or any other .xib file and drag & drop from Object Library a UIView object.

  1. For this object from Identity inspector set it's Custom Class to your recently created UIView subclass.

  1. Ready to go :]

pzcustomview's People

Contributors

zubco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.