The targeted design i want to reach is the below image :
I used the code below to create the gradient and set it to the navigation bar with the help of the function image from layer.
func image(fromLayer layer: CALayer) -> UIImage {
UIGraphicsBeginImageContext(layer.frame.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage!
}
//navbar
let gradient = CAGradientLayer()
let cornerRadiusOfNavBar : CGFloat! = 0
if #available(iOS 10.0, *) {
gradient.colors = [Constants.Colors.violet.cgColor , Constants.Colors.green.cgColor ]
}
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = self.navigationController!.navigationBar.bounds
gradient.cornerRadius = cornerRadiusOfNavBar
navigationItem.title = NSLocalizedString("Register", comment: "")
self.navigationController!.navigationBar.setBackgroundImage(self.image(fromLayer: gradient), for: .default)
and i have a stack view below the navigation bar , i used this code to set the gradient for it:
//stackview
let gradientView = CAGradientLayer()
let cornerRadiusOfStackView : CGFloat! = 0
if #available(iOS 10.0, *) {
gradientView.colors = [Constants.Colors.violet.cgColor , Constants.Colors.green.cgColor ]
}
gradientView.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientView.endPoint = CGPoint(x: 1.0, y: 0.5)
gradientView.frame = self.stackViewHoldingButtons.bounds
gradientView.cornerRadius = cornerRadiusOfStackView
self.stackViewHoldingButtons.layer.insertSublayer(gradientView, at: 0)
the result is this design below :
then i added these two lines of code to remove the navigation bar bottom border :
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
the result is as follows :
can any one help me to make the gradient homogeneous ??
P.S. : - I made sure that the navigationBar.isTranslucent property is set to false , that colors in both cases are set to sRGB , and i tried calling the lines that remove the border in both cases before and after setting the gradient.
thanks in advance.