UITableViewRowAction image for title

后端 未结 8 1815
情歌与酒
情歌与酒 2020-12-02 15:44

I made a custom UITableViewRowAction. Now I\'d like to add an image instead of the text. I know that it\'s possible but don\'t know how to do it. Does someone of you knows

8条回答
  •  不思量自难忘°
    2020-12-02 16:06

    I just implement better version of https://stackoverflow.com/a/48122210 1. In majority of cases height of cell and width of custom swipe part are different, you have to calculate this in your function 2. Image can have different size than square, so you have to calculate proportions not just for height. So, code with my fixes

    func setIcon(iconImage: UIImage, backColor: UIColor, cellHeight: CGFloat, customSwipPartWidth: CGFloat, iconSizePercentage: CGFloat) {
            let iconWidth = customSwipPartWidth * iconSizePercentage
            let iconHeight = iconImage.size.height / iconImage.size.width * iconWidth
            let marginY = (cellHeight - iconHeight) / 2 as CGFloat
            let marginX = (customSwipPartWidth - iconWidth) / 2 as CGFloat
    
    
            UIGraphicsBeginImageContextWithOptions(CGSize(width: customSwipPartWidth, height: cellHeight), false, 0)
            let context = UIGraphicsGetCurrentContext()
    
            backColor.setFill()
            context!.fill(CGRect(x:0, y:0, width:customSwipPartWidth, height:cellHeight))
    
            iconImage.draw(in: CGRect(x: marginX, y: marginY, width: iconWidth, height: iconHeight))
    
            let actionImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            self.backgroundColor = UIColor.init(patternImage: actionImage!)
        } 
    

提交回复
热议问题