OctoberHammer
5/17/2017 - 9:46 AM

GradientInBackgroundOfNavigationBarAndStatusBar

GradientInBackgroundOfNavigationBarAndStatusBar

class MainController: UINavigationController {
	
	override func viewDidLoad() {
		super.viewDidLoad()
		self.navigationBar.isTranslucent = false
		self.navigationBar.tintColor = UIColor.blue
		let fontDictionary = [ NSForegroundColorAttributeName:UIColor.red ]
		self.navigationBar.titleTextAttributes = fontDictionary
		self.navigationBar.setBackgroundImage(imageLayerForGradientBackground(), for: UIBarMetrics.default)
		
		/*if self.revealViewController() != nil {
		self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
		}*/
	}
	
	private func imageLayerForGradientBackground() -> UIImage {
		
		var updatedFrame = self.navigationBar.bounds
		// take into account the status bar
		updatedFrame.size.height += 20//Возможно дело в этом
		//0xfebf00 - более темный
		let upperColor = UIColor(red: 0xFE/255.0, green: 0xBF/255.0, blue: 0x00/255.0, alpha: 1.0);
		//0xfcda03 - более СВЕТЛЫЙ
		let lowerColor = UIColor(red: 0xFC/255.0, green: 0xDA/255.0, blue: 0x03/255.0, alpha: 1.0);
		//let layer = CAGradientLayer()
		
		let layer = CAGradientLayer.gradientLayerForBounds(bounds: updatedFrame, upperColor: upperColor, lowerColor: lowerColor)
		//            CAGradientLayer.gradientLayerForBounds(bounds: updatedFrame, startColor: UIColor(), endColor: UIColor())
		layer.bounds = updatedFrame
		
		UIGraphicsBeginImageContext(layer.bounds.size)
		layer.render(in: UIGraphicsGetCurrentContext()!)
		let image = UIGraphicsGetImageFromCurrentImageContext()
		UIGraphicsEndImageContext()
		return image!
	}
}