Skip to content

Instantly share code, notes, and snippets.

@DavidBarry
Created January 22, 2012 12:40
Show Gist options
  • Save DavidBarry/1656949 to your computer and use it in GitHub Desktop.
Save DavidBarry/1656949 to your computer and use it in GitHub Desktop.
This is a subclass of UIView to reduce boilerplate code when using Interface Builder to layout a custom UIView subclass.
#import <UIKit/UIKit.h>
@interface SDViewWithNibLayout : UIView
+ (id)viewFromNib;
+ (NSString *)nibName;
@end
#import "SDViewWithNibLayout.h"
@implementation SDViewWithNibLayout
+ (id)viewFromNib {
UINib *nib = [UINib nibWithNibName:[self nibName] bundle:[NSBundle mainBundle]];
NSArray *nibArray = [nib instantiateWithOwner:nil options:nil];
id view = nil;
for (id object in nibArray) {
if ([object isKindOfClass:[self class]]) {
view = object;
break;
}
}
return view;
}
+ (NSString *)nibName {
return NSStringFromClass([self class]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment