(Swift)如何在单元格滑动操作中设置清晰的背景? [英] (Swift) How to set clear background in cell swipe action?
问题描述
我在UITableViewCell中有一个trailingSwipeAction,其背景颜色必须清晰。
这是我设置动作的代码:
func tableView(_ tableView:UITableView,trailingSwipeActionsConfigurationForRowAt indexPath:IndexPath) - > UISwipeActionsConfiguration? {
let myAction = UIContextualAction.init(style:.normal,title:nil){
//我的行动代码
}
myAction.backgroundColor = .clear
myAction.image = UIImage.init(名称:my-icon)
返回UISwipeActionsConfiguration.init(操作:[myAction])
}
但是当我没有预期的颜色时,我会得到灰色背景:
解决方案:
你可以查看应用程序在单元格上滑动的视图层次结构,然后在Xcode中转到 Debug
菜单,然后查看调试
,并选择捕获视图层次结构
。
首先,让我们添加这个有用的扩展获取数组中的所有子视图及其子视图:
扩展UIView {
var allSubViews:[UIView] {
var array = [self.subviews] .flatMap {$ 0}
array.forEach {array.append(contentsOf:$ 0.allSubViews )}
返回数组
}
}
然后进入 viewWillLayoutSubviews()
:
覆盖func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
let btn = tableView
.allSubViews //获取所有子视图
.first(where:{String(describe:type(of:$ 0)) ==UISwipeActionStandardButton})//得到第一个是UISwipeActionStandardButton
//这个UISwipeActionStandardButton有两个子视图,在你的情况下,我得到的是一个不是UILabel的子视图,因为如果让view = btn?.subviews.first(其中:{!($ 0是UILabel)})
{$ b}你设置了图像,你应该得到一个不是imageView
的图像。 $ b view.backgroundColor = .clear //更改灰色uiview的背景颜色
}
}
我正在使用 viewWillLayoutSubviews()
因为它被调用以通知视图控制器它的视图即将布局其子视图。有关详细信息,请查看此处。
此解决方案针对一个滑动操作按钮进行了优化。如果您有多个按钮,代码将如下所示:
覆盖func viewWillLayoutSubviews(){
super .viewWillLayoutSubviews()
let buttons = tableView
.allSubViews //获取所有子视图
.filter {String(描述:type(of:$ 0))==UISwipeActionStandardButton}
buttons.forEach {btn in
if let view = btn.subviews.first(where:{!($ 0 is UIImageView)})//如果你确定除了uiview之外是UISwipeActionStandardButton的子视图中的UIImageView
{
view.backgroundColor = .clear //更改灰色uiview的背景颜色
}
}
}
I have a trailingSwipeAction in a UITableViewCell, whose background color must be clear.
This is the code where I set the action :
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let myAction = UIContextualAction.init(style: .normal, title: nil) {
// My action code
}
myAction.backgroundColor = .clear
myAction.image = UIImage.init(named: "my-icon")
return UISwipeActionsConfiguration.init(actions: [myAction])
}
But I am getting gray background for the action, when no color was expected:
SOLUTION: https://stackoverflow.com/a/52018193/9451152
You'll have to access the UIView inside the UIActionStandardButton
inside the UISwipeActionPullView
. and then change its background color.
You can see the view hierarchy of your app swiping on a cell, then going the Debug
menu in Xcode, then View Debugging
, and choose Capture View Hierarchy
.
First of all, let add this useful extension that gets all subviews and their subviews in an array:
extension UIView {
var allSubViews : [UIView] {
var array = [self.subviews].flatMap {$0}
array.forEach { array.append(contentsOf: $0.allSubViews) }
return array
}
}
And then in viewWillLayoutSubviews()
:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let btn = tableView
.allSubViews //get all the subviews
.first(where: {String(describing:type(of: $0)) == "UISwipeActionStandardButton"}) // get the first one that is a UISwipeActionStandardButton
//This UISwipeActionStandardButton has two subviews, I'm getting the one that is not a UILabel, in your case, since you've set the image, you should get the one that is not an imageView
if let view = btn?.subviews.first(where: { !($0 is UILabel)})
{
view.backgroundColor = .clear //Change the background color of the gray uiview
}
}
I am using viewWillLayoutSubviews()
since it's called to notify the view controller that its view is about to layout its subviews. Have a look here for more details.
This solution is optimized for one swipe action button. If you have more than one button, the code would look like this:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let buttons = tableView
.allSubViews //get all the subviews
.filter {String(describing:type(of: $0)) == "UISwipeActionStandardButton"}
buttons.forEach { btn in
if let view = btn.subviews.first(where: { !($0 is UIImageView)}) //If you're sure that other than the uiview there is a UIImageView in the subviews of the UISwipeActionStandardButton
{
view.backgroundColor = .clear //Change the background color of the gray uiview
}
}
}
这篇关于(Swift)如何在单元格滑动操作中设置清晰的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!