Skip to content

Instantly share code, notes, and snippets.

@rdelrosario
Created December 6, 2017 01:13
Show Gist options
  • Save rdelrosario/bff82de912c2e6273835d0280e12e892 to your computer and use it in GitHub Desktop.
Save rdelrosario/bff82de912c2e6273835d0280e12e892 to your computer and use it in GitHub Desktop.
iOS Create Gradient Background
UIImage CreateGradientBackground(Color startColor, Color endColor, CustomNavigationPage.GradientDirection direction)
{
var gradientLayer = new CAGradientLayer();
gradientLayer.Bounds = NavigationController.NavigationBar.Bounds;
gradientLayer.Colors = new CGColor[] { startColor.ToCGColor(), endColor.ToCGColor() };
switch(direction)
{
case CustomNavigationPage.GradientDirection.LeftToRight:
gradientLayer.StartPoint = new CGPoint(0.0, 0.5);
gradientLayer.EndPoint = new CGPoint(1.0, 0.5);
break;
case CustomNavigationPage.GradientDirection.RightToLeft:
gradientLayer.StartPoint = new CGPoint(1.0, 0.5);
gradientLayer.EndPoint = new CGPoint(0.0, 0.5);
break;
case CustomNavigationPage.GradientDirection.BottomToTop:
gradientLayer.StartPoint = new CGPoint(1.0, 1.0);
gradientLayer.EndPoint = new CGPoint(0.0, 0.0);
break;
default:
gradientLayer.StartPoint = new CGPoint(1.0, 0.0);
gradientLayer.EndPoint = new CGPoint(0.0, 1.0);
break;
}
UIGraphics.BeginImageContext(gradientLayer.Bounds.Size);
gradientLayer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment