Jeśli chcesz programowo ustawić gradient danym UINavigationBar mam dla Ciebie rozwiązanie.
Pierwsza konfiguracja a CAGradientLayer
z wybranymi kolorami i lokalizacjami.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = navigationBar.bounds;
gradientLayer.colors = @[ (__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor ];
gradientLayer.startPoint = CGPointMake(0.0, 0.5);
gradientLayer.endPoint = CGPointMake(1.0, 0.5);
Następnie chwyć UImage dla tej warstwy.
UIGraphicsBeginImageContext(gradientLayer.bounds.size);
[gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Ustaw gradientImage
jako backgroundImage dla UINavigationBar
.
[navigationBar setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
Swift Rozwiązanie:
[![// Setup the gradient
let gradientLayer = CAGradientLayer()
gradientLayer.frame = navigationBar.bounds
gradientLayer.colors = [UIColor.green.cgColor, UIColor.blue.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
// Render the gradient to UIImage
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Set the UIImage as background property
navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
Delegowanie to jako zasób, mam nadzieję, że jest niektóre wykorzystanie. http://stackoverflow.com/questions/19632081/horizontal-cagradientlayerlayer-in-ios-7-0 http://nscookbook.com/2013/04/recipe-20-using-cagradient-layer-in -a-custom-view/ – gwar9