Rotate UIContextualAction with swift - uitableview

I trying to make a chat, I find the way to make the tableView looks like you are filling it from bottom to top with the line of code below:
//Swipe the tableView
tableView.transform = CGAffineTransform(rotationAngle: (-.pi))
tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, tlView.bounds.size.width - 10)
//Swipe the cells
cell.transform = CGAffineTransform(rotationAngle: (-.pi))
That works nice but the problem appears when you apply an action with the function trailingSwipeActionsConfigurationForRowAt, because everything show upside down, like the image below. As you can see de "Details" button its on the right side (it must be on left side) and its upside down.
This is the function for make the action, but a UIContextualAction don't have the function transform, so, can't rotate it as the tableview and cell.
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let answerAction = UIContextualAction(style: .normal, title: "Details", handler: { (ac:UIContextualAction, view:UIView, success: #escaping (Bool) -> Void) in
success(true)
})
answerAction.backgroundColor = UIColor.white.withAlphaComponent(0)
let configuration = UISwipeActionsConfiguration(actions: [answerAction])
configuration.performsFirstActionWithFullSwipe = true
return configuration
}
So, how can I rotate? or What other way I can make the tableView fill from botton to top?
Thanks a lot for the help!

Related

Swift: Swipe action to strikethrough row in TableView

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/

UITableView multiple actions

In the editing mode of UITableView, I need three things, the first two are easy to get using UITableView delegate methods:
Delete (- in red) button on the left of the row,
Reorder (three bars) row button on the right of the row,
A custom defined action (with title & background color) appearing on the left side of reorder (three bars) button.
How is it possible to get these three actions together?
Hi yes it is possible to make or add your own custom action by implementing the tableView delegate
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let archiveAction:UITableViewRowAction = UITableViewRowAction(style: .default, title: " ") { (rowAct, index) in
}
let deleteAction:UITableViewRowAction = UITableViewRowAction(style: .default, title: " ") { (rowAct, index) in
}
let archiveImg = UIImageView(image: UIImage(named: "archive_btn"))
archiveImg.contentMode = .scaleAspectFit
archiveAction.backgroundColor = UIColor(patternImage:archiveImg.image!)
let deleteImg = UIImageView(image: UIImage(named: "delete_btn"))
deleteImg.contentMode = .scaleAspectFit
deleteAction.backgroundColor = UIColor(patternImage:deleteImg.image!)
return [deleteAction,archiveAction]
}

Remove the tableview gray background color while swipe to right in swift

I have a tableview dynamic prototype custom tableview cells.
My tableview cell color is white and the background of tableview is also white.
I want swipe to right delete feature for my tableviewcell, and with custom tableviewrowaction
I have implemented the same in my app with custom tableviewrowaction which is working fine but the only problem is when I swipe right the cell bounces and there is a gray background color behind the cell. I have removed the backgroundview, changed the color of cell and tableview but still when I swipe the cell , I can see the background color as gray.
Following is the code written to display the custom tableview delete row action
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "") { (action, indexPath) in
let cell = self.tableview.cellForRow(at: indexPath) as? CardInfoSwipeCell
cell?.deleteAction(action)
}
deleteAction.setIcon(iconImage: UIImage(named: "delete")!, backColor: UIColor.white, cellHeight: 100.0, iconSizePercentage: 0.79)
return [deleteAction]
}
Following is the code to set the image on tableviewrowaction
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: 0, y: margin, width: iconHeight - 10, height: iconHeight - 4))
let actionImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = UIColor.init(patternImage: actionImage!)
}
}
Please find the attached screenshot where a gray background gets displayed behind
Is there any way to disable the tableview dragging bounce so that it should not be dragged beyond a certain point.
I tried setting the background color to .clear but that did not work. This workaround gets the job done.
action.backgroundColor = UIColor(white: 1, alpha: 0.001)
You could use this library https://github.com/SwipeCellKit/SwipeCellKit allows you to place icons on the UITableViewRowAction
[TableView] Setting a pattern color as backgroundColor of UITableViewRowAction is no longer supported.
To disable the tableview dragging bounce you can use the following method -
#available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
// Code to perform delete action
completionHandler(true);
}
let swipeAction = UISwipeActionsConfiguration(actions:[delete])
swipeAction.performsFirstActionWithFullSwipe = false // This is the line which disables full swipe
return swipeAction
}

How can add image in UIContextualAction in UITableview DataSource method

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

custom delete image within UITabelViewCell only show half of itself

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
});

Resources