Quantcast
Channel: eppz! » XIB
Viewing all articles
Browse latest Browse all 3

Simplest way to load UIView from Xib files

0
0
Custom views are best maintainable in separate Xib files, so a convenient workflow for loading UIView from Xib is gold.

Custom views are best maintainable in separate Xib files, so a convenient workflow for loading UIView from Xib is gold.

This method is simple.

//Wow!
[[self.view addSubview:[MenuView loadFromNib]];

It takes only a single class to setup, so if you’re in a hurry just grab the files from GitHub, and take a sudden look at the Xib setup image below.

Use eppz! UIView from Xib at

The lightweight setup to load UIView from Xib

The method has three role player. First, an Interface Builder Xib file that holds a configured view we want to load. The middle player is a smart but death simple helper object EPPZViewInstantiator with the sole purpose of load a Nib file, and have a reference to a view in it. The third is a tiny UIView category (Instantiate) that uses the previous helper object to provide the lovely +loadFromNibNamed: method. Also has an alias +loadFromNib that tries to load the view from a Xib file having the class name as the file name.

In the Xib file the setup is easy. Just set the File’s Owner class to (Instantiate) and bind the view you want to load to it’s view outlet.

UIView from Xib - Just hook up the view you want to instantiate with the view outlet of File's Owner.

Just hook up the view you want to instantiate with the view outlet of File’s Owner.

You may noticed that the MenuView above is embedded into a wrapper view. By this trick you can define the position of the view in it’s superview contained in the Xib file. This setup can ensure that MenuView can have a frame property right after loaded from the Xib, so you don’t have to nudge the instance in by code in the controller that is to use MenuView.

If you feel explorous, you can see all the prequisites that lead to this solution in a previous post 5 approach to load UIView from Xib.

The post Simplest way to load UIView from Xib files appeared first on eppz!.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images