i am trying to add image in the UIContextualAction , see the code below:-
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
// Write action code for the Flag
let FlagAction = UIContextualAction(style: .normal, title: "View", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
FlagAction.image = UIImage(named: "flag")
return UISwipeActionsConfiguration(actions: [FlagAction])
}
The image , i am using
we can take size 30 by 30 , is the standard size , we can take more than 30 pixel , depending on the size of the screen.
Here is the image that is support
The UIContextualAction only accepts template image, which use different transparent degree to define the image's shape and color.
hi it is worked fine for me ,
That image size will auto adjust according to pixel of the image.
private func searchBarCode(forRowAt indexPath: IndexPath) -> UIContextualAction {
let context = UIContextualAction(style: .destructive, title: "") { (action, swipeButtonView, completion) in
completion(true)
}
context.backgroundColor = UIColor.white
context.image = UIImage(named: "document")
return context
}
Click here to see table view edit image
Related
I have a UITableView that has a different row height for the last column.
Each row has UIContextualAction to "mark as favourite" and "delete", represented by image (icons). It seams that when the row height is smaller then 50, the UIContextualAction.image placement is corrupted and no longer centered correctly:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let (training, package) = decodeIndexPath(indexPath)
return package?.trainings.last == training ? 50 : 49
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let training = decodeIndexPath(indexPath).0 else { return nil }
let imageSizeAction = CGSize(width: 30, height: 30)
var actions = [UIContextualAction]()
//add delete action (only custom exercises)
if training.isCustom {
let deleteAction = UIContextualAction(style: .destructive, title: nil) { [weak self] (action, view, completion) in
guard let training = self?.decodeIndexPath(indexPath).0 else { return }
Database.shared.deleteCustom(training: training, completion: logAsError)
completion(true)
}
deleteAction.image = PaintCode.imageOfBtnCellActionDelete(imageSize: imageSizeAction)
actions.append(deleteAction)
}
//add to favorites
let favoriteAction = UIContextualAction(style: .normal, title: nil) { [weak self] (action, view, completion) in
guard var training = self?.decodeIndexPath(indexPath).0 else { return }
training.isFavorite = !training.isFavorite
tableView.isEditing = false
completion(true)
}
favoriteAction.backgroundColor = PaintCode.mainBlue
let image = PaintCode.imageOfBtnCellActionFavorite(imageSize: imageSizeAction, selected: training.isFavorite)
favoriteAction.image = image
actions.append(favoriteAction)
let action = UISwipeActionsConfiguration(actions: actions)
//only allow full swipe if delete is added
action.performsFirstActionWithFullSwipe = actions.contains(where: {$0.style == .destructive})
return action
}
I tried using the backgroundColor and making a patternImage color I am able to get it centred correctly, however because of the tiling action you then get the icon repeated when you stretch the swipe. Not the wanted behaviour
favoriteAction.backgroundColor = UIColor(patternImage: PaintCode.imageOfBtnCellActionFavorite(imageSize:imageSize, selected: training.isFavorite))
So I see no other option then to have a min height of 50 points to make everything work reliably.
Evening ladies and gentleman,
I am currently getting used to Swift and wanted to start with a little todo app. So far I can add an item and safe it persistently in a context. When an item has been added, it will be shown in a tableview. Now, I want to use a check swipe to strikethrough items, which have been added and safe this information in my context. Deleting using a swipe works perfectly fine.
Has anybody an idea how realize this? I tried to solve it by myself, but couldnt get it done. A similar question has been asked here before, but didnt get a proper answer: Add strikethrough to tableview row with a swipe
func checkAccessoryType(cell: UITableViewCell, isCompleted: Bool) {
if isCompleted {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let todo = CoreDataManager.shared.getTodoItem(index: indexPath.row)
todo.completed = !todo.completed
CoreDataManager.shared.safeContext()
if let cell = tableView.cellForRow(at: indexPath){
checkAccessoryType(cell: cell, isCompleted: todo.completed)
}
}
Assuming you are trying to strikethrough the title of your task -- which should be defined as a label -- here is the approach to take:
1- Make sure your label is set to attributed text rather than plain. To do that, go to Main.storyboard, select your label, and inside the attribute inspector, set text to Attributed.
2- Inside your completion block (that is the completion block executed after a swipe) add the following code:
(SWIFT 5)
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: taskLabel.text)
attributeString.addAttribute(.strikethroughStyle, value: 1, range: NSRange(location: 0, length: taskLabel.text.count))
taskLabel.attributedText = attributeString
Just a little advice: it's always helpful if you add some code when you ask a question.
Let me know if anything doesn't make sense.
Looking at the link that you provided, you need swipe action on your UITableViewCell.
Try looking into:
leadingSwipeActionsConfigurationForRowAt
trailingSwipeActionsConfigurationForRowAt
You need this action to perform the strikethrough label or delete:
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Closed")
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [closeAction])
}
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Update", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
modifyAction.image = UIImage(named: "hammer")
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}
Source: https://developerslogblog.wordpress.com/2017/06/28/ios-11-swipe-leftright-in-uitableviewcell/
swipe table view cell then show some option to delete and edit. I want to set full image. I have seen lots of demo code but then are with text and background image, I have need to create with whole image here is my code for ios 10 and ios 11 but I cant get success
with editActionsForRowAt Problem is image is repeate multiple time
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let ArchiveAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: " ") { (action , indexPath ) -> Void in
tableView.setEditing(false, animated: false)
}
let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: " ") { (action , indexPath) -> Void in
tableView.setEditing(false, animated: false)
}
ArchiveAction.backgroundColor = UIColor(patternImage: UIImage(named: "archiver.png")!)
shareAction.backgroundColor = UIColor(patternImage: UIImage(named: "bloquear.png")!)
return [ArchiveAction,shareAction]
}
with trailingSwipeActionsConfigurationForRowAt Problem is image not show properly. show white image
#available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "sdfsdf", handler: { (action,view,completionHandler ) in
//do stuff
completionHandler(true)
})
// action.image = UIImage(named: "archiver.png")
action.backgroundColor = .black
let confrigation = UISwipeActionsConfiguration(actions: [action])
confrigation.performsFirstActionWithFullSwipe = true // default is false
return confrigation
}
Please give me any solution
I also experienced the problem of a white rectangle instead of the image when using .jpg. Using a .png worked for me.
Since you are using a .png already, did you try to load the image name without the .png extension? ("archiver" instead of "archiver.png")
Because the parameter description of init?(named name: String) says:
... For PNG images, you may omit the filename extension. For all other file formats, always include the filename extension.
I've set up a destructive trailing swipe action under iOS 11:
#available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, completionHandler) in
self.deleteRow(atIndexPath: indexPath)
completionHandler(true)
})
// Is it possible here?
//deleteAction.image = UIImage(named: "delete")
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
}
Instead of the "delete" text, I'd like to show the Apple system icon thrash bin found here:
https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/system-icons/
Obviously, I could obtain the icon from somewhere and set it as the image, but I guess there should be a native way to do this.
Thanks for your suggestions!
I want to custom my delete button of the UITableViewCell like these codes:
let deleteAciont = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: nil, handler: {action, indexpath in
do some thing
});
deleteAciont.backgroundColor = UIColor(patternImage: UIImage(named: "delete")!)
the height of the UITableViewCell is 70 like this:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70
}
and the images are these size: delete.png:70*70\delete#2x.png:140*140\delete#3x.png:210*210
but when i swipe left, the image only show half:
I have been confusing about this for a long time and thanks for your help
the length of the tableViewRowAction will be the title's length
let deleteAciont = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: " type more space here ", handler: {action, indexpath in
do some thing
});