Search code examples
iphoneiosxcodeios5xcode4.2

Editing Custom UIView Subclasses in Storyboard


I have an application that uses a UIScrollView to page between a number of dynamically generated UIViews. Right now, I have to programmatically set the layout and ui elements of these UIViews, but I was wondering if there is any way to edit the interface of a subclass of UIView in storyboard (independent of a view controller).

Thanks in advance.

Edit: Just for reference, I'm using Xcode 4.2 with iOS 5.


Solution

  • UPDATE

    As of Xcode 7, you can edit a standalone view in a storyboard. Drag a view from the Object Library to the header bar of a view controller. Interface Builder will show the standalone view separately on the canvas above the view controller's main view. Example:

    standalone view in storyboard

    You will probably want to create an outlet from your view controller to the standalone view. The standalone view will only be accessible from the view controller that contains it.

    Note that you get one instance of the standalone view when you load the view controller. If you need multiple instances, there's no particularly good way to get them.

    ORIGINAL

    There is no convenient way to do this in a storyboard.

    You can still create nibs in a project that uses a storyboard. So you can create a nib for each view, and load them from your code:

    NSString *nibName = [NSString stringWithFormat:@"page%d", pageNumber];
    NSArray *nibObjects = [NSBundle.mainBundle loadNibNamed:nibName owner:self options:nil];
    UIView *pageView = [nibObjects objectAtIndex:0];
    CGSize size = self.scrollView.bounds.size;
    pageView.frame = CGRectMake(pageNumber * size.width, 0, size.width, size.height);
    [self.scrollView addSubview:pageView];