iOS Question How to set a gradient color on a button

JanPRO

Well-Known Member
Licensed User
Longtime User
B4X:
SetButtonGradient(Button1,Colors.Black,Colors.White)

Sub SetButtonGradient(aButton As Button, Color1 As Int, Color2 As Int)
    Dim NaObj As NativeObject = Me
    NaObj.RunMethod("SetGradient:::",Array(aButton,NaObj.ColorToUIColor(Color1),NaObj.ColorToUIColor(Color2)))
End Sub

#If OBJC

- (void)SetGradient: (UIButton*) Button :(UIColor*) Color1 :(UIColor*) Color2{
    CAGradientLayer *Gradient = [CAGradientLayer layer];
    Gradient.colors = [NSArray arrayWithObjects: (id)Color1.CGColor, (id)Color2.CGColor, nil];
    Gradient.frame = Button.bounds;

    [Button.layer insertSublayer:Gradient atIndex:0];
}
#End If

;)
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
Hello

The function works but after I'd like come back with a linear color.

if I do

button.color = Colors.White
or
SetButtonGradient(button,Colors.White,Colors.White)


The color does not change.
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Oh yes, you are right.
Before setting the color, call this sub:
B4X:
Sub RemoveSubLayers(aButton As Button)
    Dim NaObj As NativeObject = aButton
    NaObj.GetField("layer").SetField("sublayers",Null)
End Sub
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
It crashs in RemoveSubLayers

I do

B4X:
SetButtonGradient(BTN_0,Colors.ARGB(0xFF,0x5B,0x8F,0xEA),Colors.ARGB(0xFF,0x19,0x47,0xEF))
RemoveSubLayers(BTN_0)

Sub SetButtonGradient(aButton As Button, Color1 As Int, Color2 As Int)
    Dim NaObj As NativeObject = Me
    NaObj.RunMethod("SetGradient:::",Array(aButton,NaObj.ColorToUIColor(Color1),NaObj.ColorToUIColor(Color2)))
End Sub
#If OBJC

- (void)SetGradient: (UIButton*) Button :(UIColor*) Color1 :(UIColor*) Color2{
    CAGradientLayer *Gradient = [CAGradientLayer layer];
    Gradient.colors = [NSArray arrayWithObjects: (id)Color1.CGColor, (id)Color2.CGColor, nil];
    Gradient.frame = Button.bounds;

    [Button.layer insertSublayer:Gradient atIndex:0];
}
#End If


Sub RemoveSubLayers(aButton As Button)
    Dim NaObj As NativeObject = aButton
    NaObj.GetField("layer").SetField("sublayers",Null)
End Sub
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
Copying updated assets files (11)
Application_Start
Application_Active
SignalHandler 11
Error occurred on line: 109 (code_selectdimmer)
Signal - 11
Stack Trace: (
"0 RDM Remote SignalHandler + 76",
"1 libsystem_platform.dylib 0x3906ff8b _sigtramp + 34",
"2 UIKit <redacted> + 86",
"3 UIKit <redacted> + 528",
"4 UIKit <redacted> + 532",
"5 UIKit <redacted> + 118",
"6 UIKit <redacted> + 380",
"7 QuartzCore <redacted> + 142",
"8 QuartzCore <redacted> + 350",
"9 QuartzCore <redacted> + 16"
)
stop program
Stop program
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
that's work but it crashs
if I do

SetButtonGradient(BTN_0,Colors.ARGB(0xFF,0x5B,0x8F,0xEA),Colors.ARGB(0xFF,0x19,0x47,0xEF)) 'BLUE
RemoveSubLayers(BTN_0)
SetButtonGradient(BTN_0,Colors.ARGB(0xFF,0xFF,0x00,0x00),Colors.ARGB(0xFF,0xFF,0x00,0x00)) ' RED


The button changes color but it crashs
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
I created a small project

The app crashs sometimes


The origin color is yellow ( set in visual designer)


Each pushing the color changes between Blue and Red.

Issue :
1) The blue color is small
2) The text disappared
3) After +- 30 seconds without touch and 1 push, it crashs
 

Attachments

  • gradientcolor.zip
    2.5 KB · Views: 183
Upvote 0
Top