iphoneobjective-ciosios5

iOS 5 Create a custom view


I was wondering what is the best approach in creating a view such as the one on the left hand side of the following link?

I know how to make apps that use the templates in the storyboard but kind of lost on the guideline when it comes to creating custom views. Does it all get created at runtime using code? Do I need to add one subview per section (Gift, Birthday,...) to my main view? any general approach for creating such a view is appreciated.

example interface
(source: fastcodesign.com)


Solution

  • At the top level it looks like they have a UIToolbar and a UITableView or a UIScrollView. They have used some custom graphics in the toolbar; possibly they have implemented their own toolbar class.

    The table view is probably using a custom UITableViewCell subclass. It is using variable height rows (by defining the tableView:heightForRowAtIndexPath: method in the delegate). They might just be using a scroll view instead of a table view, but I'll assume it's a scroll view for this discussion.

    Each cell has appears to have at least three subviews: one to draw the cell's title bar, one to draw the cell's contents, and a UIPageControl to draw the page dots under the contents.

    The contents part of the cell looks like it's probably a scroll view, with some subviews to draw images (UIImageView) and labels (UILabel). The subviews of the scroll view are quite different for the different table view cells.

    You could lay out an interface like this using nibs. I'd probably use four nibs: one for the top-level with the toolbar and the table view, one nib for the table view cell, one nib for the gift layout (which has a UILabel over a UIImageView), and one nib for the person/date layout (which has a UIImageView to the left of three UILabels).

    You need to set some properties in code. For example, suppose you have one nib for the overall layout of a table view cell. It probably has a view hierarchy like this:

    UITableViewCell (or subclass)
        UIView to provide the colored stripe across the top
            UIImageView for the icon
            UILabel for the title (“Browse Gifts”, “Birthdays”, etc.)
            UIButton for the disclosure indicator
        UIView to provide the white background with shadow
            UIScrollView to hold the main content of the cell
            UIPageControl
    

    When you load this nib to use for your “Browse Gifts” cell, you need to set the shadow properties of the white background view's layer, because you can't do that in the nib. You need to set the color of the stripe, the icon, and the title text of the cell on the appropriate views. You need to add content to the scroll view (which probably involves loading another nib once for each content item). You need to set the number of pages on the page control.