How to change UITableViewRowAction title color?

后端 未结 10 859
梦如初夏
梦如初夏 2020-11-30 05:49

Am using UITableViewRowAction in \"editActionsForRowAtIndexPath\" method. I can change the backgroundcolor of UITableViewRowAction, but am not able to change the title color

相关标签:
10条回答
  • 2020-11-30 06:34

    Lee Andrew's answer in Swift 3 / Swift 4:

    class MyCell: UITableViewCell {
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            for subview in self.subviews {
    
                for sub in subview.subviews {
    
                    if String(describing: sub).range(of: "UITableViewCellActionButton") != nil {
    
                        for view in sub.subviews {
    
                            if String(describing: view).range(of: "UIButtonLabel") != nil {
    
                                if let label = view as? UILabel {
    
                                    label.textColor = UIColor.black
    
                                }
    
                            }
    
                        }
    
                    }
    
                }
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 06:36
    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
        {
        // Action something here
    
    }];
    
    editAction.backgroundColor = [UIColor whiteColor];
    [[UIButton appearance] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    return @[editAction];
    
    0 讨论(0)
  • 2020-11-30 06:40

    You can add these two functions in your UITableViewCell subclass and call the setActionButtonsTitleColor function to set action buttons' title color.

    func setActionButtonsTitleColor(color: UIColor) {
      let actionButtons: [UIButton] = self.getActionButtons()
    
      for actionButton in actionButtons {
        actionButton.setTitleColor(color, for: .normal)
      }
    }
    
    func getActionButtons() -> [UIButton] {
      let actionButtons: [UIButton] = self.subviews.map {
        (view: UIView) -> [UIView] in
        return view.subviews
      }
      .joined()
      .filter {
        (view: UIView) -> Bool in
        return String(describing: view).contains("UITableViewCellActionButton")
      }.flatMap {
        (view: UIView) -> UIButton? in
        return view as? UIButton
      }
    
      return actionButtons
    }
    
    0 讨论(0)
  • 2020-11-30 06:43

    Swift

    No need to mess with UIButton.appearance...

    Put this in your cell's class and change UITableViewCellActionButton according to your needs.

    override func layoutSubviews() {
    
        super.layoutSubviews()
    
        for subview in self.subviews {
    
            for subview2 in subview.subviews {
    
                if (String(subview2).rangeOfString("UITableViewCellActionButton") != nil) {
    
                    for view in subview2.subviews {
    
                        if (String(view).rangeOfString("UIButtonLabel") != nil) {
    
                            if let label = view as? UILabel {
    
                                label.textColor = YOUR COLOUR
                            }
    
                        }
                    }
                }
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题