mardi 23 juin 2015

Get the current width of an UICell

I have a custom cell (subclass of UITableViewCell) and I have some UITextField in it whose relative position within the cell I want to choose.

So my code resembles that :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSInteger offset = 230;
        UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(offset, 10, 95, 30)];
        [self.contentView addSubview:myTextField];
    }
    return self;
}

Now my problem is that this works fine in portrait mode but in landscape mode I would like to adjust offset to simulate a right align of my textfield. Similarly, I would like to be able to adjust for an iPad.

How can I get the current width of my cell ?

I tried to use self.contentView.frame.size.width and self.contentView.bounds.size.width but both always return 320.

I know it sounds like a question that would have been asked thousand times but I haven't found anything...

NB : I am aware there will be issues to handle with reusing cells but I have not gotten to this point yet.

See more ..

Aucun commentaire:

Enregistrer un commentaire