ios - 如何将渐变层放置到没有框架的 UIImageView 上

标签 ios swift xcode uiimageview gradient

我试图在我的 iOS 应用程序中的图像上放置灰色透明渐变,但由于框架问题,它不起作用。由于我的 UIImageView 是以编程方式设置的,并且使用了 translatesAutoresizingMaskIntoConstraints = false,所以当我将它的框架设置为图像框架时,渐变层不可见。我不想将任一帧设置为常量,因为图像位于 Collection View 单元格中,该单元格使用 sizeForItemAt 函数来确定我的每个单元格的大小,从而确定图像的大小随着单元格的大小而变化,我需要我的渐变与图像具有相同的大小。

我试图通过创建一个 UIView 来设置渐变,我将它作为 UIImageView 的 subview 放置,但由于框架再次出现问题而没有成功.我看过其他关于添加渐变图层的帖子,但它们都要求我为渐变定义框架。

//This is the imageview and the gradient that I want to apply to it
let profileImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    } ()
var gradient: CAGradientLayer = {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor.clear.cgColor,    UIColor.gray.cgColor]
        gradientLayer.opacity = 0.7
        return gradientLayer
    }()

//This is the setupviews() function where I apply the gradient to the imageview
func setupViews() {
        self.contentView.addSubview(profileImageView)
        profileImageView.layer.addSublayer(gradient)

        //constraints for profileImageView
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[v0]-0-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": profileImageView]))

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[v0]-0-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": profileImageView]))
}

目前,为了显示渐变,我需要在 setupViews() 中为渐变层定义一个框架,但正如我之前所述,我宁愿不这样做。有没有一种方法可以在不定义特定框架的情况下创建渐变层,或者是否有另一种更好的方法在我的案例中应用渐变层?谢谢!

编辑:我想澄清一下,我的 UICollectionViewCell 类中的所有 View 都没有框架。它们都是以编程方式设置约束的,所以如果我尝试打印 view.frame.heightview.frame.width 程序将打印零,尽管实际高度和宽度的观点不为零。因此,为渐变定义任何类型的框架或将渐变的框架设置为 View 的框架将不起作用。为了能够在图像上放置渐变,我是否必须重新构建 View 的设置方式?或者我还能希望从这个问题中得到拯救吗?谢谢!

最佳答案

您可以: - 使用以下代码创建一个 Swift 文件 - 将您的 imageView 设置为此类 - 将此 imageView 或 UIView(在这种情况下相应地转动类)放在包含您的图像的 UIImageView 上方。 它应该可以工作(参见示例屏幕截图)。 根据您的颜色/alpha 设置修改参数。 希望对您有所帮助。

import Foundation
import UIKit

class AddGradientsToImageView: UIImageView {

// For item created programmatically
override init(frame: CGRect) {
    super.init(frame: frame)

    addGradientsToImageView()
}

// For items created in the storyboard
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    addGradientsToImageView()
}

func addGradientsToImageView() {

// Set background gradient colors
let lightBlue = UIColor(red: 112/255, green: 117/255, blue: 239/255, alpha: 1)
let darkBlue = UIColor(red: 70/255, green: 84/255, blue: 201/255, alpha: 1)

// Set gradients
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [darkBlue.cgColor, lightBlue.cgColor]
gradientLayer.locations = [0.0,1.0]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)

// Clip gradients to background bounds
clipsToBounds = true

// Set transparency level
alpha = 0.50

// insert gradients to button
layer.insertSublayer(gradientLayer, at: 0)
}
}

result of the code to this imageView result of the code with a picture behind

关于ios - 如何将渐变层放置到没有框架的 UIImageView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55999091/

相关文章:

ios - Swift Parse - 在两个类中创建新记录

swift - 您如何在 Swift 3 中记录函数闭包参数的参数?

xcode - 如何在 Xcode 6 和 Storyboard 中使用 NSToolBar?

iphone - 截取部分屏幕的屏幕截图

android - 使用 animateToRegion() 与 animateCamera() 的优缺点

swift - 以每秒特定速率获取帧位置

ios - 单例改变整数

iOS下载批量图像的最佳方式

ios - 从在 iOS 8 中工作但在 iOS 7 中不工作的已保存屏幕截图设置 UIImageView 图像

swift - 为什么在尝试转换图像时 URL 无法正常工作?