sparrowcode / PermissionsKit

Universal API for request permission and get its statuses.
https://x.com/sparrowcode_ios
MIT License
5.64k stars 462 forks source link

Having trouble setting the icon tint color when using custom icon #180

Closed sdevo619 closed 4 years ago

sdevo619 commented 4 years ago

What is the syntax for setting the icon tint color, when using custom supplied icons??

I have tried setting the cell tint and also tried using the cell.iconView.color, both before and after setting the image using cell.set(UIIImage) and still not working. The icons I am using are all template images, so not sure what I am doing wrong here.

ivanvorobei commented 4 years ago

Please, share code which you use

sdevo619 commented 4 years ago

None of what I have tried has work, so an example probably won't help much. Just use a custom icon for any of the persmissions, the icon is set as template icon in the assets folder, and try to set the tint color of it in SPPermissionTableViewCell. I need the icon to be able to be custom, and to set a custom color.

func configure(_ cell: SPPermissionTableViewCell, for permission: SPPermission) -> SPPermissionTableViewCell {

    guard let name = name(for: permission), let description = description(for: permission), let image = image(for: permission) else{

        print("error with Permission item")

        return cell}

    cell.permissionTitleLabel.text = name
    cell.permissionDescriptionLabel.text = description
    cell.iconView.color = .red

    cell.set(image)

    return cell

}

Also, setting the color before or after setting the image has not made a difference. I want my custom icon to be tinted red, but it is still shows the default blue tint color. How do I fix this?? Thank you very much!!!

ivanvorobei commented 4 years ago

For now you can set only color with similar button in allow state. It ok for you? If yes, you need set rendering mode to template before setting image.

If you want special color, need pass valid image. Icon view color change color only for system icons.

sdevo619 commented 4 years ago

I have already done that with all of these icons I am having issue with, and it is still not working correctly. The template images keep defaulting to the system default tint, which is blue. I need the image to be the same color as the allow color. I have tried setting the tintColor on the base view, the cell view, the cell content view, and none have worked. I believe this to be an issue with you code that is easily fixable. I don't think that the imageView you are using, when using a custom, non system icon, is adopting the cells tint properties, so maybe if you just set that imageView, to use the cell tintColor, it would be an easy way to implement this, as it will still allow user to provide non template images too. What do you think??? //using this doesn't currently fix the issue, but would be an easy to fix this, if you add the necessary code on your end cell.tintColor = .green cell.contentView.tintColor = .green

Screen Shot 2020-04-17 at 1 06 33 PM

image

ivanvorobei commented 4 years ago

I change protect level to public for icon view. You can not set tint color in configure method. Don't forget update to 5.3.2 version, it latest.

sdevo619 commented 4 years ago

Thank you for making the change, and the help, but I am not sure how that will help fix it.

I dug through the SPPermission Library and this was how I got it work, incase anyone else asks you. The cell.set() method is what was causing the issue for me, as in it, the tint color of iconImageView.tintColor is not being updated, at least in your version.

//Your version of the method public func set(_ image: UIImage) { iconView.isHidden = true iconImageView.isHidden = false iconImageView.image = image }

This is the line that fixed this cell.iconImageView.tintColor = .red

//The more proper fix for this would be something like this:

public func set( image: UIImage, tintColor: UIColor = .blue) { iconView.isHidden = true iconImageView.isHidden = false iconImageView.image = image

    iconImageView.tintColor = tintColor
}
sdevo619 commented 4 years ago

Thank your for your help on this today!!!!

ivanvorobei commented 4 years ago

For fitst, use set method for image. For second, you not set rendering mode and tint color not work without it.

sdevo619 commented 4 years ago

I don't understand what you just said, but it is working now with the fix I put in place, that I mention in my last post. The last updated method I included was a more permenant fix you could make for this. You can leave it as is and it will work fine, but the last method will help with this issue I had for other people who use this library, as the fix is not elegant, as it doesn't make sense to set the cell.imageView.tintColor, when the the cell.set() method is replacing that iconView with the iconImageView. If you use the cell.set() method to replace that image, it should also handle setting the tint Color of the new image or return an image that can easily have the tint set for it, but I thought this seemed more logical as setting the tint Color of an image that isn't templated, does nothing, so as long as the image is setup right in the asset folder template vs non template, this will work great. I hope this make sense. Thank you for all your help on this!!! Keep up the great work!!! Would love to see you add HomeKit permission to this too. I know it is a bit more work to do than a normal permission request, but I know you can figure it out. Best of luck if you do!!!!