Yes, you can do it, but beware nib files are stored on disk instead of in RAM so it is ~10,000 times slower than not using a nib. If you are creating a lot of views, don't do it this way.
Basically your init method loads the nib file, grabs the view in the nib (which should be defined as the same class), and the instead of returning self like most init methods, it returns the view in the nib.
If you lookup the language documentation you will see it's common for an init method to return a different object than "self".
If you are not using ARC, you must release self and retain the view you are returning.
Here is how I would implement it (nib file must match the class name):
- (instancetype)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;
NSArray *loadedObjects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:@{}];
id newView = nil;
for (id loadedObject in loadedObjects) {
if ([loadedObject isMemberOfClass:[self class]]) {
newView = loadedObject;
break;
}
}
// if ARC is disabled, you must do this:
// [self release];
// [newView retain];
self = newView;
self.frame = frame;
return self
}