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.
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.
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.
DISCLAIMER. THE INFORMATION ON THIS BLOG (INCLUDING BUT NOT LIMITED TO ARTICLES, IMAGES, CODE SNIPPETS, SOURCE CODES, EXAMPLE PROJECTS, ETC.) IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE INFORMATION ON THIS BLOG (INCLUDING BUT NOT LIMITED TO ARTICLES, IMAGES, CODE SNIPPETS, SOURCE CODES, EXAMPLE PROJECTS, ETC.).Tweet