Swift NavigationBar 渐变与下方视图不同
Swift NavigationBar gradient different than on view below
我在 naviBar 和下面的视图上有渐变,但颜色显示不同。有人知道为什么吗?我没有看到额外的色调。
导航栏代码:
func addGradientImageView(withColours: [UIColor] = [.amaranth, .dodgerBlue]) {
let cgColours = withColours.map { [=11=].cgColor }
let gradientLayer = CAGradientLayer()
var updatedFrame = bounds
updatedFrame.size.height += UIApplication.shared.statusBarFrame.height
gradientLayer.frame = updatedFrame
gradientLayer.colors = cgColours
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
setBackgroundImage(image, for: UIBarMetrics.default)
}
查看代码:
func applyHorizontalGradient(withColours: [UIColor] = [.amaranth, .dodgerBlue]) {
let cgColours = withColours.map { [=12=].cgColor }
let grad = CAGradientLayer()
grad.startPoint = CGPoint(x: 0, y: 0)
grad.endPoint = CGPoint(x: 1, y: 0)
grad.frame = self.bounds
grad.colors = cgColours
self.layer.insertSublayer(grad, at: 0)
}
extension UIView {
func applyGradientImage(withColours: [UIColor] = [.amaranth, .dodgerBlue],
startPoint: CGPoint = CGPoint(x: 0, y: 0),
endPoint: CGPoint = CGPoint(x: 1, y: 0)) {
let imageView = ImageView()
insertSubview(imageView, at: 0)
imageView.expandToSuperviewSafearea(withoutBottomSafeArea: false)
imageView.image = gradientImage(withColours: withColours, startPoint: startPoint, endPoint: endPoint)
}
}
我在 naviBar 和下面的视图上有渐变,但颜色显示不同。有人知道为什么吗?我没有看到额外的色调。
导航栏代码:
func addGradientImageView(withColours: [UIColor] = [.amaranth, .dodgerBlue]) {
let cgColours = withColours.map { [=11=].cgColor }
let gradientLayer = CAGradientLayer()
var updatedFrame = bounds
updatedFrame.size.height += UIApplication.shared.statusBarFrame.height
gradientLayer.frame = updatedFrame
gradientLayer.colors = cgColours
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
setBackgroundImage(image, for: UIBarMetrics.default)
}
查看代码:
func applyHorizontalGradient(withColours: [UIColor] = [.amaranth, .dodgerBlue]) {
let cgColours = withColours.map { [=12=].cgColor }
let grad = CAGradientLayer()
grad.startPoint = CGPoint(x: 0, y: 0)
grad.endPoint = CGPoint(x: 1, y: 0)
grad.frame = self.bounds
grad.colors = cgColours
self.layer.insertSublayer(grad, at: 0)
}
extension UIView {
func applyGradientImage(withColours: [UIColor] = [.amaranth, .dodgerBlue],
startPoint: CGPoint = CGPoint(x: 0, y: 0),
endPoint: CGPoint = CGPoint(x: 1, y: 0)) {
let imageView = ImageView()
insertSubview(imageView, at: 0)
imageView.expandToSuperviewSafearea(withoutBottomSafeArea: false)
imageView.image = gradientImage(withColours: withColours, startPoint: startPoint, endPoint: endPoint)
}
}