美文网首页
UITableViewCell左滑按钮的字体颜色的修改

UITableViewCell左滑按钮的字体颜色的修改

作者: 龙之天下 | 来源:发表于2022-03-28 21:21 被阅读0次

    1、iOS 10 层级图

    (1)、TableViewCell --->UITableViewCellDeleteConfirmationView --->  _UITableViewCellActionButton ---> UIButtonLabel
    (2)、 _UITableViewCellActionButton : UIButton
    (3)、可以将_UITableViewCellActionButton 强转为UIButton
    

    2、 iOS 11 层级图

    (1)、TableView --->  UISwipeActionPullView  --->  UISwipeActionStandardButton  --> UIButtonLabel
    (2)、UISwipeActionStandardButton : UISwipeActionButton : UIButton
    (3)、UIButton 可能含有UIButtonLabel的属性
    (4)、可以将UISwipeActionStandardButton 强转为UIButton
    

    3、iOS 15 层级图

    (1)、TableView ---> _UITableViewCellSwipeContainerView  --->  UISwipeActionPullView  --->  UISwipeActionStandardButton  --> UIButtonLabel
    (2)、UISwipeActionStandardButton : UISwipeActionButton : UIButton
    (3)、UIButton 可能含有UIButtonLabel的属性
    (4)、可以将UISwipeActionStandardButton 强转为UIButton
    

    4、获取iOS 11.0的 UIButton

    ///iOS 11.0 UIButton
    @available(iOS 11.0, *)
    private func getSwipeActionStandardButton(tableView: UITableView) -> UIButton? {
        let subviews = tableView.subviews
        for subview in subviews {
            if let anyCl = NSClassFromString("_UITableViewCellSwipeContainerView"), subview.isKind(of: anyCl) {
                for subview1 in subview.subviews {
                    return getSwipeActionStandardButton(subview: subview1)
                }
            }else {
                return getSwipeActionStandardButton(subview: subview)
            }
        }
        return nil
    }
    

    5、获取iOS10(包括 iOS 10) 以下的 UIButton

    ///iOS11 以下 UIButton
    private func getTableViewCellActionButton(cell: UITableViewCell) -> UIButton? {
        for subview in cell.subviews {
            if let anyCl = NSClassFromString("UITableViewCellDeleteConfirmationView"), subview.isKind(of: anyCl) {
                for subview1 in subview.subviews {
                    if let button = subview1 as? UIButton {
                        return button
                    }
                }
            }
        }
        return nil
    }
    

    6、最终结果

    ///最终结果
    private func getTableSwipeButton(tableView: UITableView, indexPath: IndexPath) -> UIButton? {
        if #available(iOS 11.0, *) {
            return getSwipeActionStandardButton(tableView: tableView)
        }else {
            //下面代码没有测试
            if let cell = tableView.cellForRow(at: indexPath) {
                return getTableViewCellActionButton(cell: cell)
            }
        }
        return nil
    }
    

    7、执行位置

    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        if let button = getTableSwipeButton(tableView: tableView, indexPath: indexPath) {
            print("button:====\(button)")
            button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
            button.setTitleColor(UIColor.red, for: .normal)
        }
    }

    相关文章

      网友评论

          本文标题:UITableViewCell左滑按钮的字体颜色的修改

          本文链接:https://www.haomeiwen.com/subject/kagkjrtx.html