答案 0 :(得分:1)
如果您在View Controller中,请在viewDidLoad()中或在适当的位置编写以下代码。
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.view.bounds
let color1 = UIColor(red: 238.0/255.0, green: 54.0/255.0, blue: 97.0/255.0, alpha: 1.0).CGColor as CGColorRef
let color2 = UIColor(red: 238.0/255.0, green: 89.0/255.0, blue: 59.0/255.0, alpha: 1.0).CGColor as CGColorRef
gradientLayer.colors = [color1, color2]
gradientLayer.locations = [0.0, 0.5]
self.view.layer.addSublayer(gradientLayer)
使用图像中的颜色选择器拾取color1和color2。请使用准确的RGB值作为最顶部和最底部的颜色。
答案 1 :(得分:0)
最好的方法是始终使用Image作为背景
let navBackgroundImage:UIImage! = UIImage(named: "your_background_image_name_here")
如果您只想设置视图控制器
self.navigationController.navigationBar.setBackgroundImage(navBackgroundImage,
forBarMetrics: .Default)
如果您要为所有屏幕设置,请在AppDelegate的didFinishLaunchingWithOptions
方法
[UINavigationBar .appearance().setBackgroundImage(navBackgroundImage, forBarMetrics:.Default)]
答案 2 :(得分:0)
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:152.0/255.0 green:185.0/255.0 blue:252.0/255.0 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:38.0/255.0 green:63.0/255.0 blue:123.0/255.0 alpha:1.0] CGColor], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
通过添加零(0)索引的子层为我工作。