Determine the size of a View by inspecting its NIB

I was exploring the UICollectionView API tonight and stumbled upon an old problem. Both UITableView and UICollectionView ask their delegate for size information about the cells they display. When creating cells in separate XIB files, which I normally do, this means that the delegate must somehow know how big the root View is in that XIB. The easiest way to do that is evil: hard-code the cell’s size in the delegate. Duplication! Yuck!! No more!!!

It dawned on me that this entire problem can be avoided by doing a one-time lookup of the View’s natural size, as it exists in the XIB file (and, at run-time, in the view’s NIB). I posted a Gist on GitHub that shows my simple solution:

Happy coding!

About Josh Smith

Josh Smith is a musician at heart, and a software developer by obsession. He lives in Arizona, plays classical music on the piano, and enjoys a variety of tasty beverages. He has a wonderful wife and thinks that, in general, life is good.
This entry was posted in Interface Builder, Tips and Tricks, UICollectionView, UITableView. Bookmark the permalink.

One Response to Determine the size of a View by inspecting its NIB

  1. delta search says:

    The other day, while I was at work, my sister
    stole my apple ipad and tested to see if it can survive a 40
    foot drop, just so she can be a youtube sensation.
    My apple ipad is now broken and she has 83 views.
    I know this is entirely off topic but I
    had to share it with someone!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s