簡體   English   中英

如何在自定義單元格中居中放置圖像

[英]How can I center image in custom cell

在我的自定義單元格中,我想使圖像居中,但這不起作用。

    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];

嘗試設置leftImageautoresizingMask

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

創建單元格時,您永遠不確定其尺寸是多少。 通常,將使用CGRectZero框架初始化單元格,然后, UITableView將適當地調整單元格的大小。

我認為您在創建新單元格或在init部分的單元格本身中嘗試在- (UITableViewCell *)tableView:(UITableView *)tableViewRef cellForRowAtIndexPath:(NSIndexPath *)indexPath執行此操作。 目前,單元格沒有正確的框架 ,您不能依靠它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM