我收到一条日志消息
[TableView]不再支持将图案颜色设置为UITableViewRowAction的backgroundColor。
在iOS 11之前,图案颜色运行良好。
我的项目使用一个向左滑动的自定义表格视图来执行操作,在行操作背景色中,我使用100x100图像。
例如:
editAction.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LeaveRequest_Eng.png"]];
我无法完全展示那张照片;图像被拉伸并且没有填充所有区域。
我相信苹果正在鼓励/迫使您现在使用新的UIContextualAction
api。以前,许多开发人员会使用colorWithPatternImage:
向UITableViewRowAction
添加图像,这总是有点像黑客攻击和脆弱。从iOS 11开始,现在有一种支持的方式可以通过实现- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView
trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
将图像添加到行操作中。例如:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView
trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction *editAction = [UIContextualAction contextualActionWithStyle:UIControlStateNormal title:@"Edit" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"Do editing");
completionHandler(YES);
}];
editAction.image = [UIImage imageNamed:@"MyEditImage"];
editAction.backgroundColor = [UIColor blueColor];
return [UISwipeActionsConfiguration configurationWithActions:@[editAction]];
}
以下是相同的快速解决方案:
@available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let message = self.messages[indexPath.row]
let deleteAction = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
// Perform your action here
//self.deleteMessage(message: message)
completion(true)
}
let muteAction = UIContextualAction(style: .normal, title: "Mute") { (action, view, completion) in
// Perform your action here
completion(true)
}
deleteAction.image = #imageLiteral(resourceName: "CheckedMarkRead")
deleteAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [deleteAction, muteAction])
}