我做了一个自定义的UITableViewRowAction。现在我想添加图片而不是文字。我知道这是可能的,但不知道如何去做。 您是否有人知道如何在Swift中做到这一点,并想帮助我? 感谢您的回答!UITableViewRowAction图像标题
回答
您需要的UIImage设置排行动的backgroundColor,具体是:
斯威夫特:
UIColor(patternImage: UIImage(named: ""))
的Objective-C:
[UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
感谢它的工作! – user3592462 2015-04-03 15:58:33
对于图像,我放了一个CocoaPod,试图尽可能优雅地解决这个问题:https://github.com/benguild/BGTableViewRowActionWithImage – 2015-08-21 16:54:05
@BenGuild,谢谢 – 2015-12-23 04:56:13
delActions.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"delete.png"]];
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]
}
仅供参考这不会让你只是添加一个图像,也必须有文字。当我将“”放入标题参数时(由于强制解开titleLabel.text),它崩溃了。我不得不把“”放入标题参数来获得一个只有滑动动作的图像......在正面,你可以通过空格的数量来控制按钮的宽度...... :) – Natalia 2016-09-09 18:57:49
@Jayesh Miruliya,how你知道UITableViewRowAction有一个按钮属性?这不在文档中:https://developer.apple.com/reference/uikit/uitableviewrowaction。这在技术上是黑客? – etayluz 2016-09-19 16:02:02
不适用于iOS 8.4 :( – Jenny 2016-09-23 04:29:37
问题与基本的图案颜色的方法是你的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
正如您所看到的,您可以在代码中设置按钮的背景和色调颜色,而不是在艺术品本身中。
我做了这个简单的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!)
}
}
- 1. 更改UITableViewRowAction swift的标题3
- 2. iOS 8 UITableViewRowAction:向左滑动,图像?
- 3. Dismiss UITableViewRowAction
- 4. UITableViewRowAction accessibilityValue
- 5. jquery图像标题
- 6. 图标图像的问题
- 7. UITableViewRowAction在Swift中自定义标题字体大小
- 8. 旋转图像标题与图像
- 9. UITableView为图像图标添加标题
- 10. 在图像标签问题中的图标图像在IE中
- 11. 标题的背景图像
- 12. carouFredSel当前图像标题
- 13. 标题在UIButton与图像
- 14. 图像滑块标题
- 15. 用rvest刮图像标题
- 16. 标题返回图像
- 17. 重复标题图像CSS
- 18. OWL-CAROUSEL2带图像标题
- 19. 如何访问UITableViewRowAction的视图iOS 8
- 20. jquery mobile中图像标题的问题
- 21. UITableViewRowAction处理单击
- 22. 图标图像 - 透明度问题
- 23. css图标图像或图标图像?
- 24. 在UITableView中的标题标题旁边添加图像
- 25. 调整响应引导图像与标题标题文本
- 26. 将表中的图像对齐并添加标题标题行
- 27. Rails回形针显示图像标题,而不是图像
- 28. 在C++中读取RAW图像或做RAW图像有标题?
- 29. 如何创建图像标题和图像叠加与CSS?
- 30. 标志图像漂浮在图像滑块WordPress的主题CSS
在这里找到答案-https://stackoverflow.com/questions/44771778/how-to-add-image-in-uitableviewrowaction/45301272#45301272 – Jack 2017-07-25 10:57:35
更新回答两个iOS的11和早期 https://开头stackoverflow.com/questions/27740884/uitableviewrowaction-title-as-image-icon-instead-of-text/46337919#46337919 – 2017-09-21 07:50:27