簡體   English   中英

關閉壓縮單元格背景圖像-Objective-C(Xcode)

[英]Turn Off Compressing Cell Background Image - Objective-C (Xcode)

我正在使用以下代碼將內容添加到UITableViewCell:

NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"title"]]];
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:[device valueForKey:@"imagename"]] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

但是,盡管此代碼有效,但它會壓縮我的圖像以適合單元格。 我希望圖像僅顯示圖像的頂部,而根本不拉伸或壓縮。

使用[UIView setContentMode:]

模式如下:

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};

您可以使用topLeft。

添加:

具有頂帽高度的StretchableImage將使您的圖像適合您的視圖。 我認為您無法顯示圖片的頂部。

如果只需要顯示圖像的頂部,請嘗試此操作。

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[device valueForKey:@"imagename"]]];
[iv setContentMode:UIViewContentModeTop];
[iv setClipsToBounds:YES];
cell.backgroundView = iv;

將您的最后一行代碼更改為此,然后嘗試。

暫無
暫無

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

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