Web Analytics Made
Easy - StatCounter

Menu

About us Contact us

iOS Custom UIView from .xib

Tech 2019 / 07 / 16

iOS Custom UIView from .xib



Creating CustomView.xib file

Setting view’s file owner

• The fileowner is the object that loads the nib, i.e. fileowner receives the message loadNibName: or initWithNibName: .
If we want to access any objects in the nib after loading it, you can set an outlet in the file owner. ViewController acts as fileowner for View in storyboard that is why we don’t explicitly set fileowner for view in storyboard.

• The reason it is called file owner and given a special place, is because unlike the other objects in the nib, the file owner is external to the nib and is not part of nib. In fact, it only becomes available when the nib is loaded. So the file owner is a stand-in or proxy for the actual object which will later load the nib.

• So you created a fancy view with lots of subviews eg: buttons, labels, etc . If you want to modify any of these subviews any time after loading the nib, we set outlets for these objects to the fileowner.

• Whenever we create custom view from xib we set its fileowner not custom sub class.

Drawing outlet from a view to its file owner

.xib and it’s corresponding class `CustomView`

https://gist.githubusercontent.com

Drawing acting for button

https://gist.githubusercontent.com

Setting custom view as subclass from storyboard

https://gist.githubusercontent.com

2. The second way we can use CustomView is programmatically as shown below. This time init(frame: CGRect) get called instead of init?(coder aDecoder: NSCoder) as we called the init(frame: CGRect) method explicitly.

https://gist.githubusercontent.com

Demo

Demo of Custom View

https://github.com/rokonuddin/CustomView

Thank you all for your attention 🙏🏻. feel free to tweet and get connected.

 


Related Posts :

iOS TableView Prefetching DataSource using Swift

Step into Swift Combine

You have ideas, We have solutions.

CONTACT US!