Reputation: 937
I'm currently developing a Swift 4 iOS application. I have a custom table cell that is displayed in a card view (see example screenshot below).
When I implement SwipeActions on a row, the height of the background color of the swipe menu takes up the full height of the cell row.
I'd like for it to be just the height of the card view. I should add that each row is a different height as they contain small pieces of text of varying length. Is there a way to achieve this?
Upvotes: 2
Views: 1921
Reputation: 21
Based on PollyVern answer, this is what works for me.
extension UITableViewCell {
var cellActionPullView: UIView? {
superview?.subviews
.filter{ String(describing: $0).range(of: "UISwipeActionPullView") != nil }
.compactMap { $0 }.first
}
func setRemoveSwipeAction() {
cellActionPullView?.frame.size.height = self.frame.height - 8
cellActionPullView?.frame.origin.y = 8
}
}
override func layoutSubviews() {
super.layoutSubviews()
setRemoveSwipeAction()
}
override func layoutIfNeeded() {
super.layoutIfNeeded()
setRemoveSwipeAction()
}
Upvotes: 2
Reputation: 79
And what if all the cells in the application are written using UIEdgeInsets with top and bottom? And you need to implement a swipe action of the desired height. I hope the answer will help someone.
Advantages of this method, no need to calculate the height of the cell (suitable for a dynamic cell).
Add in extension UITableViewCell this UIView and method. Set this parameters of cell, as top and bottom.
extension UITableViewCell {
var cellActionPullView: UIView? {
superview?.subviews
.filter{ String(describing: $0).range(of: "UISwipeActionPullView") != nil }
.compactMap { $0 }.first
}
func setRemoveSwipeAction() {
cellActionPullView?.snp.makeConstraints({ make in
make.top.equalTo(self.snp.top).offset(8)
make.bottom.equalTo(self.snp.bottom).inset(8)})
}
}
Add in your cell:
override func layoutSubviews() {
super.layoutSubviews()
setRemoveSwipeAction()
}
override func layoutIfNeeded() {
super.layoutIfNeeded()
setRemoveSwipeAction()
}
Add in your VC leadingSwipeActionsConfigurationForRowAt or trailingSwipeActionsConfigurationForRowAt.
Result:
Upvotes: 3
Reputation: 31
Exists an alternative to do this. You can change size, but it is old mode like change height (view.size.height= (newvalue))
. To change position use (view.frame.origin.y = (newvalue))
. It will work in static height cell and iOS >=11. This will not work in dynamic height cells. Make these changes inside the delegate method of tableviewcell
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? TempcellTableViewCell{
if let spvcell = cell.superview{
for svswipe in spvcell.subviews{
let typeview = type(of: svswipe.self)
if typeview.description() == "UISwipeActionPullView"{
svswipe.frame.size.height = 100//size you want
svswipe.frame.origin.y = 5
}
}
}
}
}
and in functions of leadingSwipeActionsConfigurationForRowAt/trailingSwipeActionsConfigurationForRowAt
. Make following changes before it return UISwipeActionsConfiguration object.
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let Cancel = UIContextualAction.init(style: .normal, title: "Cancel") { (action, view, nil) in
print("Cancel")
}
Cancel.backgroundColor = UIColor.red
let config = UISwipeActionsConfiguration.init(actions: [Cancel])
config.performsFirstActionWithFullSwipe = false // Prevent swiping
tableView.reloadRows(at: [indexPath], with: .none)
return config
}
This solution worked for me :D ... I hope this answer can help you
Upvotes: 2
Reputation: 937
This isn't possible. Find an alternative approach for customization.
Upvotes: -2