Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Xcode file templates for nib - loadable UIView subclass.

NotificationsYou must be signed in to change notification settings

zubco/PZCustomView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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)    }}

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

FancyCustomView.swiftFancyCustomView.xib

So, for nibName is enough to have:

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

In this case thenibName will haveFancyCustomView 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 stagecontentView and any other outlets get their values from .xib

AndMore

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. UIViewControllerwith view
  2. UITableViewCell / UICollectionViewCellwith contentView

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

By defaultcontentView 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 fromcommonInit.

Only customization needed tocontentView observed in code is:

contentView.frame = bounds

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

How to install

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

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

How to use

  1. Access from Xcode menuNew File option.
  2. IniOS tab,User interface category findCustom 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 fromObject Library a UIView object.

  1. For this object fromIdentity inspector set it'sCustom Class to your recently created UIView subclass.

  1. Ready to go :]

About

Xcode file templates for nib - loadable UIView subclass.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp