3

我在 Swift 3 中有一个项目,我在视图上使用渐变。我在视图上放置了一个图像,但它位于渐变层之下,所以我打算以编程方式而不是使用情节提要来绘制图像。我似乎无法弄清楚如何使图像居中并使图像适合视图的边界。

是)我有的...

//Gradient Background
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.view.frame
gradientLayer.colors = [UIColor(red: 0/255, green: 147/255, blue: 255/255, alpha: 1.0).cgColor, UIColor(red: 162/255, green: 134/255, blue: 255/255, alpha: 1.0).cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
self.view.layer.addSublayer(gradientLayer)

//Image
let mainImage = UIImage(named:"WhiteLogoFront")
var mainImageView = UIImageView(image:mainImage)
self.view.addSubview(mainImageView)
4

2 回答 2

10

你可以试试看

let mainImage = UIImage(named:"WhiteLogoFront")
    var mainImageView = UIImageView(image:mainImage)
    mainImageView.contentMode = .ScaleAspectFit
于 2017-02-17T05:40:36.250 回答
1
 let gradientLayer = CAGradientLayer()
    gradientLayer.frame = self.view.frame
    gradientLayer.colors = [UIColor(red: 0/255, green: 147/255, blue: 255/255, alpha: 1.0).cgColor, UIColor(red: 162/255, green: 134/255, blue: 255/255, alpha: 1.0).cgColor]
    gradientLayer.locations = [0.0, 1.0]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
    self.view.layer.addSublayer(gradientLayer)

    //Image
    let mainImage = UIImage(named:"WhiteLogoFront")
    var mainImageView = UIImageView(image:mainImage)
    mainImageView.center = self.view.cente
    mainImageView.contentMode = .ScaleAspectFit
    self.view.addSubview(mainImageView)

试试这个

于 2017-02-17T06:23:56.527 回答