iOS Switch « ON » color

  1. 2 months ago

    Olivier C

    Mar 14 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    Hi,

    I have an app with lots of iOS Switches and the customer wants to something else than the standard green when some switches are ON.

    Changing the background (using declares) doesn’t work, it’s a rectangle (square corners) behind the switch which also replaces the white color inside the switch when it’s OFF. When ON the green color
    Changing TintColor doesn’t work: affect the border color when switch is OFF and has no effect when switch ON.
    -image-
    Has anyone a solution to change this green color ?

    Cherry on the cake would be to be able to specify a color for OFF state and another for ON.

    Thanks !

    You might wanna try iosdesignextentions as it should have the "ontintcolor" property for switches.

    See here:
    https://github.com/jkleroy/iOSDesignExtensions/blob/master/README.md

    And here:
    https://github.com/jkleroy/iOSDesignExtensions

    From iosDesignExtentions:

    Sub SetOnTintColorXC(extends s As iOSSwitch, value As Color)
    		  
    	 'declare sub setTintColor lib "UIKit.framework" selector "setTintColor:" (id as ptr, UIColor as Ptr)
    	 'setTintColor s.Handle, new UIColor(c)
    		  
    	 Dim uic As UIKit.UIColor
    	 If value.Alpha = 255 Then
    	   uic = UIKit.UIColor.ClearColor
    	 Else
    	   uic = New UIColor(value)
    	 End If
    		  
    	 declare sub setOnTintColor lib "UIKit.framework" selector "setOnTintColor:" (id as ptr, UIColor as Ptr)
    	 setOnTintColor(s.Handle, uic)
    End Sub

    you need the complete class/module to have UIColor and such but this is how it works.

  2. Derk J

    Mar 14 Pre-Release Testers, Xojo Pro

    you need the property "onTintColor" (when on) instead of "TintColor" (when off) you might still wanna use the declares as there is no alternative.
    https://developer.apple.com/documentation/uikit/uiswitch/1623687-ontintcolor

  3. @Olivier C — Apparently, you need to set the "cornerRadius" to remove the unwanted corners.

    See thist

  4. Derk J

    Mar 14 Pre-Release Testers, Xojo Pro Answer
    Edited 2 months ago

    You might wanna try iosdesignextentions as it should have the "ontintcolor" property for switches.

    See here:
    https://github.com/jkleroy/iOSDesignExtensions/blob/master/README.md

    And here:
    https://github.com/jkleroy/iOSDesignExtensions

    From iosDesignExtentions:

    Sub SetOnTintColorXC(extends s As iOSSwitch, value As Color)
    		  
    	 'declare sub setTintColor lib "UIKit.framework" selector "setTintColor:" (id as ptr, UIColor as Ptr)
    	 'setTintColor s.Handle, new UIColor(c)
    		  
    	 Dim uic As UIKit.UIColor
    	 If value.Alpha = 255 Then
    	   uic = UIKit.UIColor.ClearColor
    	 Else
    	   uic = New UIColor(value)
    	 End If
    		  
    	 declare sub setOnTintColor lib "UIKit.framework" selector "setOnTintColor:" (id as ptr, UIColor as Ptr)
    	 setOnTintColor(s.Handle, uic)
    End Sub

    you need the complete class/module to have UIColor and such but this is how it works.

  5. Olivier C

    Mar 14 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    Thanks for these ideas, I’ll check later today and let you know !

  6. Dave S

    Mar 14 San Diego, California USA

    onTIntColor is the "ON" color
    tintColor is the outside border when "OFF"
    thumbTintColor is the color of the thumb (I think this is only for iOS10+)

    using any of these there is no need to mess with the cornerRadius

  7. @Dave S — True but the cornerRadius is useful if you want to change the background color because the background is a rectangle by default. At least that is what I understood from the article I linked. I am no specialist.

  8. Dave S

    Mar 14 San Diego, California USA

    @Stéphane ;Mons @Dave S — True but the cornerRadius is useful if you want to change the background color because the background is a rectangle by default. At least that is what I understood from the article I linked. I am no specialist.

    Personally I think APPLE muffed this one.... tintColor should change the internal color for "off" not leave it transparent

  9. @Dave Sisemore — I agree with you.

  10. Olivier C

    Mar 14 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    Thanks to all, Jeremie extensions are perfect for ON Tint color.
    Best regards

or Sign Up to reply!