2015-04-02 35 views
26

我做了一個自定義的UITableViewRowAction。現在我想添加圖片而不是文字。我知道這是可能的,但不知道如何去做。 您是否有人知道如何在Swift中做到這一點,並想幫助我? 感謝您的回答!UITableViewRowAction圖像標題

+0

在這裏找到答案-https://stackoverflow.com/questions/44771778/how-to-add-image-in-uitableviewrowaction/45301272#45301272 – Jack 2017-07-25 10:57:35

+0

更新回答兩個iOS的11和早期 https://開頭stackoverflow.com/questions/27740884/uitableviewrowaction-title-as-image-icon-instead-of-text/46337919#46337919 – 2017-09-21 07:50:27

回答

37

您需要的UIImage設置排行動的backgroundColor,具體是:

斯威夫特:

UIColor(patternImage: UIImage(named: "")) 

的Objective-C:

[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]; 
+0

感謝它的工作! – user3592462 2015-04-03 15:58:33

+3

對於圖像,我放了一個CocoaPod,試圖儘可能優雅地解決這個問題:https://github.com/benguild/BGTableViewRowActionWithImage – 2015-08-21 16:54:05

+0

@BenGuild,謝謝 – 2015-12-23 04:56:13

0
delActions.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"delete.png"]]; 
6
class TableViewRowAction: UITableViewRowAction 
{ 
    var image: UIImage? 

    func _setButton(button: UIButton) 
    { 
     if let image = image, let titleLabel = button.titleLabel 
     { 
      let labelString = NSString(string: titleLabel.text!) 
      let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font]) 

      button.tintColor = UIColor.whiteColor() 
      button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal) 
      button.imageEdgeInsets.right = -titleSize.width 
     } 
    } 
} 


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
{ 
    let delete = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "   ") { action, indexPath in } 
    delete.image = UIImage(named: "trashImg") 

    let sharing = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "   ") { action, indexPath in } 
    sharing.backgroundColor = UIColor.lightGrayColor() 
    sharing.image = UIImage(named: "sharingImg") 

    return [delete, sharing] 
} 
+0

僅供參考這不會讓你只是添加一個圖像,也必須有文字。當我將「」放入標題參數時(由於強制解開titleLabel.text),它崩潰了。我不得不把「」放入標題參數來獲得一個只有滑動動作的圖像......在正面,你可以通過空格的數量來控制按鈕的寬度...... :) – Natalia 2016-09-09 18:57:49

+1

@Jayesh Miruliya,how你知道UITableViewRowAction有一個按鈕屬性?這不在文檔中:https://developer.apple.com/reference/uikit/uitableviewrowaction。這在技術上是黑客? – etayluz 2016-09-19 16:02:02

+0

不適用於iOS 8.4 :( – Jenny 2016-09-23 04:29:37

0

問題與基本的圖案顏色的方法是你的IM年齡需要與動作按鈕的大小相同,或者至少在底部有一些更平坦的背景以防止圖像重複(即使它將錨定在頂部,這並不是很好)。

我不得不處理動態高度的細胞,使我實現了以下內容:

- (UIColor *)backgroundImageForActionAtIndexPath:(NSIndexPath *)indexPath withImage:(UIImage *)image tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgrounfColor expectedWith:(CGFloat)width { 
// 
CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath]; 
CGSize expectedSize = CGSizeMake(width, cellFrame.size.height); 

UIGraphicsBeginImageContextWithOptions(expectedSize, NO, 0.0); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
if (ctx) { 
    // set the background 
    CGContextSetFillColorWithColor(ctx, backgrounfColor.CGColor); 
    CGRect fillRect = CGRectZero; 
    fillRect.size = expectedSize; 
    CGContextFillRect(ctx, fillRect); 
    // put the image 
    CGContextSetFillColorWithColor(ctx, tintColor.CGColor); 
    CGRect rect = CGRectMake((expectedSize.width - image.size.width)/2., (expectedSize.height - image.size.height)/2., image.size.width, image.size.height); 
    [image drawInRect:rect]; 
} 

UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return [UIColor colorWithPatternImage:newImage]; 

}

你仍然需要設置expectedWidth所以你把行動的標題的空標籤相匹配。例如:@「」 - > 64.f

正如您所看到的,您可以在代碼中設置按鈕的背景和色調顏色,而不是在藝術品本身中。

1

我做了這個簡單的UITableViewRowAction類別,以便爲我的操作設置圖標。 您可以設置圖像,背景顏色,單元格高度(管理動態單元格)和圖標大小百分比。

extension UITableViewRowAction { 

    func setIcon(iconImage: UIImage, backColor: UIColor, cellHeight: CGFloat, iconSizePercentage: CGFloat) 
    { 
    let iconHeight = cellHeight * iconSizePercentage 
    let margin = (cellHeight - iconHeight)/2 as CGFloat 

    UIGraphicsBeginImageContextWithOptions(CGSize(width: cellHeight, height: cellHeight), false, 0) 
    let context = UIGraphicsGetCurrentContext() 

    backColor.setFill() 
    context!.fill(CGRect(x:0, y:0, width:cellHeight, height:cellHeight)) 

    iconImage.draw(in: CGRect(x: margin, y: margin, width: iconHeight, height: iconHeight)) 

    let actionImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    self.backgroundColor = UIColor.init(patternImage: actionImage!) 
    } 
}