Set horizontal gradient to view

hatzisn

Well-Known Member
Licensed User
Here is a slight modification to Ilan's code for setting vertical gradient. This code gives horizontal gradient:

B4X:
Sub SetGradientHorizontal(v As View, color1 As Int, color2 As Int)
    Dim NaObj As NativeObject = Me
    NaObj.RunMethod("SetGradientHorizontal:::",Array(v,NaObj.ColorToUIColor(color1),NaObj.ColorToUIColor(color2)))
End Sub

#If OBJC
- (void)SetGradientHorizontal: (UIView*) View :(UIColor*) Color1 :(UIColor*) Color2{
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.startPoint = CGPointMake(0.0, 0.5);
    gradient.endPoint = CGPointMake(1.0, 0.5);
    gradient.colors = [NSArray arrayWithObjects:(id)Color1.CGColor, (id)Color2.CGColor, nil];
    gradient.frame = View.bounds;
    [View.layer insertSublayer:gradient atIndex:0];
}
#end if
 
Top