pvllnspk
pvllnspk

Reputation: 5767

self.view.size in a custom view returns [0,0]

I have my custom view:

@interface MailAttributesView : UIView 
{
...


@implementation MailAttributesView
{
    UIView *_selectionView;
}
(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        DLog(@"frame.size.width -[%f]; frame.size.height -[%f] ",frame.size.width, frame.size.height);
    }
    return self;
}

-(void)awakeFromNib
{
    [self initView];
}

-(void)initView
{
    DLog(@"self.frame.size.width -[%f]; self.frame.size.height -[%f] ",self.frame.size.width, self.frame.size.height);

    CGRect sectionSize = CGRectMake(0, 0 , self.frame.size.width, self.frame.size.height);
    _selectionView = [[UIView alloc] initWithFrame:sectionSize];
    [_selectionView setBackgroundColor:[UIColor clearColor]];

    fromField = [[JSTokenField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, ROW_HEIGHT)];
    [[fromField label] setText:@"  From:"];
    [fromField setDelegate:self];
    [_selectionView addSubview:fromField];

In my storyboard I've created a view and set it my custom view class. Problem that in my view I call self.view to define width of the view - but it always returns 0. I can hardcore needed values - but I expect to get it working like standard controls work - I mean for example auto-sizing if rotate and so on.

What's my misunderstanding?

Upvotes: 0

Views: 284

Answers (1)

Eric
Eric

Reputation: 5681

If you are adding the custom view in storyboards, you would not be using initWithFrame:

Instead use initWithCoder:

Try this:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        DLog(@"self.frame.size.width -[%f]; self.frame.size.height -[%f] ",self.frame.size.width, self.frame.size.height);
    }

    return  self;
}

Upvotes: 2

Related Questions