Created
December 6, 2017 01:13
-
-
Save rdelrosario/bff82de912c2e6273835d0280e12e892 to your computer and use it in GitHub Desktop.
iOS Create Gradient Background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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