如何在UITableViewRowAction中添加图像?
我试图在UITableView
滑动样式中添加图像。 我试着用Emoji文字和它的工作正常
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let editAction = UITableViewRowAction(style: .normal, title: "🖋") { (rowAction, indexPath) in print("edit clicked") } return [editAction] }
但我需要图像而不是表情符号,同时我尝试了
editAction.backgroundColor = UIColor.init(patternImage: UIImage(named: "edit")!)
但它得到了重复的图像,我使用了许多格式的图像,如20 * 20,25 * 25,50 * 50,但仍然复制。
我如何添加图片?
最后,在iOS 11中 , SWIFT 4我们可以在UISwipeActionsConfiguration
帮助下,在UITableView的滑动操作中添加图像
@available(iOS 11.0, *) func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let action = UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler ) in //do stuff completionHandler(true) }) action.image = UIImage(named: "apple.png") action.backgroundColor = .red let confrigation = UISwipeActionsConfiguration(actions: [action]) return confrigation }
WWDCvideo28.34
苹果文件
注意:我已经使用了50 * 50点的apple.png图片和50个tableview
row height
这是我如何在objective-c
做到这一点,翻译时应该swift
工作。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *deleteString = @"Delete"; CGFloat tableViewCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath]; UIImage *image = [UIImage imageNamed:@"delete_icon"]; CGFloat fittingMultiplier = 0.4f; CGFloat iOS8PlusFontSize = 18.0f; CGFloat underImageFontSize = 13.0f; CGFloat marginHorizontaliOS8Plus = 15.0f; CGFloat marginVerticalBetweenTextAndImage = 3.0f; float titleMultiplier = fittingMultiplier; NSString *titleSpaceString= [@"" stringByPaddingToLength:[deleteString length]*titleMultiplier withString:@"\u3000" startingAtIndex:0]; UITableViewRowAction *rowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleSpaceString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ //Do Stuff }]; CGSize frameGuess=CGSizeMake((marginHorizontaliOS8Plus*2)+[titleSpaceString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:iOS8PlusFontSize] } context:nil].size.width, tableViewCellHeight); CGSize tripleFrame=CGSizeMake(frameGuess.width*3.0f, frameGuess.height*3.0f); UIGraphicsBeginImageContextWithOptions(tripleFrame, YES, [[UIScreen mainScreen] scale]); CGContextRef context=UIGraphicsGetCurrentContext(); [[UIColor blueColor] set]; CGContextFillRect(context, CGRectMake(0, 0, tripleFrame.width, tripleFrame.height)); CGSize drawnTextSize=[deleteString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize] } context:nil].size; [image drawAtPoint:CGPointMake((frameGuess.width/2.0f)-([image size].width/2.0f), (frameGuess.height/2.0f)-[image size].height-(marginVerticalBetweenTextAndImage/2.0f)+2.0f)]; [deleteString drawInRect:CGRectMake(((frameGuess.width/2.0f)-(drawnTextSize.width/2.0f))*([[UIApplication sharedApplication] userInterfaceLayoutDirection]==UIUserInterfaceLayoutDirectionRightToLeft ? -1 : 1), (frameGuess.height/2.0f)+(marginVerticalBetweenTextAndImage/2.0f)+2.0f, frameGuess.width, frameGuess.height) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize], NSForegroundColorAttributeName: [UIColor whiteColor] }]; [rowAction setBackgroundColor:[UIColor colorWithPatternImage:UIGraphicsGetImageFromCurrentImageContext()]]; UIGraphicsEndImageContext(); return @[rowAction]; }
我在SWIFT 3中find了一个这样做的方法 –
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { //let cell = tableView.cellForRow(at: indexPath) //print(cell?.frame.size.height ?? 0.0)//hence we need this height of image in points. make sure your contentview of image is smaller let deleteAction = UITableViewRowAction(style: .normal, title:" ") { (rowAction, indexPath) in print("delete clicked") } deleteAction.backgroundColor = UIColor(patternImage:UIImage(named: "delete")!) return [deleteAction] }
我们需要确保我们的图像尺寸与细胞行高度匹配这是我使用的图像
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let write = UITableViewRowAction(style: .default, title: "\u{1F58A}") { action, index in print("edit button tapped") } return [write] }
试试这个使用的Unicode代替图标。 这将工作
- Xcode 8:使用iOS 9.3基础SDK进行编译?
- 当我用UIView覆盖所有屏幕时,如何用UIView覆盖UIStatusBar? (苹果手机)
- 关于唯一标识符的iOS7应用向后兼容iOS5
- iOS模拟器声音
- 警告ITMS-90080:“可执行文件”Payload / myapp.app / Frameworks / some-framework.framework“不是位置独立可执行文件
- 截断在单词或字符边界处包含表情符号或unicode字符的string
- 什么时候应该在swift中使用UIButton的任何对象?
- 如果一个文本字段已经添加到单元格,怎么不创build一个新的文本字段
- 我的第一个开源项目