Set horizontal gradient to view

Discussion in 'iOS Code Snippets' started by hatzisn, Dec 30, 2018.

  1. hatzisn

    hatzisn Active Member Licensed User

    Here is a slight modification to Ilan's code for setting vertical gradient. This code gives horizontal gradient:

    Sub SetGradientHorizontal(v As View, color1 As Int, color2 As Int)
    Dim NaObj As NativeObject = Me
    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
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice