2

I have a UIView inside a UIViewController that I've created using the interface builder. I want to share and reuse that UIView across my app. The UIView will always have the same design and will only change its data.

What I already know:

  • I can build the UIView by code and re-add it to each UIViewController - not a solution for me, I want to create the UIView using the interface builder.

  • I can use an external xib like suggested in this answer: https://stackoverflow.com/a/11444019/1578927 I tried it and it works great but I wonder, isn't there a better solution for reusing a UIView across my storyboard app? maybe without using a xib? I voted up on the solution above but it feels kinda hacky.

Thanks

Community
  • 1
  • 1
Segev
  • 19,035
  • 12
  • 80
  • 152

2 Answers2

0

There is no way you can reuse a UIView using storyboard, what you need to do is subclass UIView [say we name it to CustomView] and in (void)awakeFromNib method you can addSubViews and do the designing stuff like, set the background colour to UIView.

You can make use of this SubClass in storyboard, reusing it to render the same UIView for any UIViewController you want, you have to just set the class from UIView to CustomView in the IdentityInspector of UIView of a UIViewController.

Sample Custom UIView class

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    UIImage *bgImage = [[UIImage imageNamed:@"bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0)];

    UIImageView *bgImageView;

    bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, viewHeight(self))];

    [bgImageView setImage:bgImage];

    [self addSubview:bgImageView];    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
Say2Manuj
  • 709
  • 10
  • 20
  • Read the first * in my question. I'm looking for a way to do it and still design the `UIView` using the interface builder – Segev Jul 05 '13 at 09:01
  • I figured it out, instead of using awakeFromNib, you can add a nib file for the CustomView. and then use this CustomView class instead of UIView in the file inspector of the respective UIViewController http://i40.tinypic.com/a9widx.jpg – Say2Manuj Jul 05 '13 at 09:30
  • That's the second * in my question. You should really take the time to read questions thoroughly. – Segev Jul 05 '13 at 09:37
0

This would be impossible. The storyBoard is made of xib(nib) files. Your custom view that you are making in storyBoard is going to be a xib file. You can't "not" use xib files or "not" do it by code because those are your only two options. Using the external xib as in the example you provided would be your best bet.

ksealey
  • 1,698
  • 1
  • 17
  • 16