I have a subclass of UIView that needs to calculates it's height according to it's width. When created in code everything works. However when I try to create the view in Interface builder, and although I override all related methods, I can't get the size of the view set in interface builder.
- (id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"init with coder before super width %d",super.frame.size.width); // returns 0
self = [super initWithCoder:aDecoder];
NSLog(@"init with coder after super width %d",super.frame.size.width); // still returns 0
}
- (void) awakeFromNib
{
NSLog(@"width of view %d",super.frame.size.width); // Returns 0 as well
}
- (void) setFrame:(CGRect)aFrame
{
[super setFrame:aFrame]; // Called from initWithCoder by super. Correct frame size.
}
So my next guess was the maybe the superview of my view is setting my view's frame after awakeFromNib. Well it turns out it doesnt. I overrided setFrame on my view, and it is called during initWithCoder.
So this is what I know so far:
Any help will be appreciated
If you extend initWithCoder
, be sure to call the super method. It is during this super call that setFrame
will be called on your class.
You can then re-use your standard initWithFrame
call.
I always do the following:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder]; // Required. setFrame will be called during this method.
return [self initWithFrame:[self frame]];
}