开发者

How can I center image in custom cell

In my custom cel开发者_开发百科l I want to center an image but this does not work.

    self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ;
    float h=cell.frame.size.height;
    float w=cell.frame.size.width;

    CGRect r = leftImage.frame;
    r.origin = cell.frame.origin;
    r.origin.x = w/ 2  - r.size.width / 2;
    r.origin.y = h / 2  - r.size.height / 2 + 12;
    leftImage.frame = r;
            [self.contentView addSubview:leftImage];


I think You tried to do this in - (UITableViewCell *)tableView:(UITableView *)tableViewRef cellForRowAtIndexPath:(NSIndexPath *)indexPath when creating new cell or in cell itself in init section. At this moments cell don't have correct frame and you can't rely on it.


Try setting the leftImage's autoresizingMask:

self.leftImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin

When the cell is created, you're never sure what its dimensions are. Mostly, a cell will be initialized with a CGRectZero frame and later on, the UITableView will resize the cell appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜