Guest User

Untitled

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. class BaseViewController: UIViewController {
  2.  
  3. var navigationBarColors: Array<UIColor> {
  4. get {
  5. return [UIColor.black, UIColor.lightGray]
  6. }
  7. }
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. applyGradient(to: (navigationController?.navigationBar)!)
  12. }
  13.  
  14. func applyGradient(to navigationBar: UINavigationBar) {
  15. let gradient = createGradientLayer(withColors: navigationBarColors, view:navigationBar)
  16. navigationBar.setBackgroundImage(image(fromLayer: gradient), for: .default)
  17. applyFontColor(with: navigationBarColors[0], navigation: navigationBar)
  18. }
  19.  
  20. func applyFontColor(with color: UIColor, navigation navigationBar: UINavigationBar) {
  21. UIApplication.shared.statusBarStyle = color.isLight() ? .default : .lightContent
  22. navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: color.isLight() ? UIColor.black : UIColor.white]
  23. }
  24.  
  25. func createGradientLayer(withColors colors: Array<UIColor>, view navigationBar: UINavigationBar) -> CAGradientLayer {
  26. let gradient = CAGradientLayer()
  27. gradient.frame = navigationBar.bounds
  28. gradient.colors = [colors[0].cgColor, colors[1].cgColor]
  29. gradient.startPoint = CGPoint(x: 0, y: 0)
  30. gradient.endPoint = CGPoint(x: 1, y: 0)
  31. return gradient
  32. }
  33.  
  34. func image(fromLayer layer: CAGradientLayer) -> UIImage {
  35. UIGraphicsBeginImageContext(layer.bounds.size)
  36. layer.render(in: UIGraphicsGetCurrentContext()!)
  37. let image = UIGraphicsGetImageFromCurrentImageContext()
  38. UIGraphicsEndImageContext()
  39. return image!
  40. }
  41. }
Add Comment
Please, Sign In to add comment