ios - View 渐变的背景颜色

标签 ios swift view gradient

我对 tableViewCell 中的 View 有疑问。所以我有一个 tableViewCell,在里面我有一个 UIView,我称之为“containerView”。现在我希望 containerView 的背景是从浅灰色到深灰色的渐变。在下面的代码中,您可以看到我是如何设置图层和所有内容的,但是由于某种原因,带有渐变的“backgroundColor”有点偏向右侧和向下。希望你能帮我解决这个问题,这是我第一次使用渐变,所以请耐心等待 :D

import UIKit

class customCell: UITableViewCell {

@IBOutlet var containerView: UIView!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let layerView = UIView()
    layerView.layer.frame = containerView.layer.frame
    let layer = CAGradientLayer()
    layer.frame = containerView.layer.frame
    layer.colors = [UIColor.lightGrayColor().CGColor, UIColor.darkGrayColor().CGColor]
    layerView.layer.addSublayer(layer)
    containerView.addSubview(layerView)
    containerView.sendSubviewToBack(layerView)

  }
}

最佳答案

这就是我将渐变颜色指定为自定义单元格背景的方式

let startingColorOfGradient = UIColor(colorLiteralRed: 217/255, green: 90/255, blue: 57/255, alpha: 1.0).CGColor
    let endingColorOFGradient = UIColor(colorLiteralRed: 227/255, green: 214/255, blue: 42/255, alpha: 1.0).CGColor
    let gradient: CAGradientLayer = CAGradientLayer()

    gradient.frame = bounds
    gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
    gradient.endPoint = CGPoint(x: 0.5, y:1.0)
    gradient.colors = [startingColorOfGradient , endingColorOFGradient]
    // backGroundView is a View I have placed on my cell as container
    self.backGroundView.layer.insertSublayer(gradient, atIndex: 0)      

关于ios - View 渐变的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112977/

相关文章:

ios - 在另一个类中滑动手势识别器

swift - nill 在分配数据标签时(在展开 Optional 值时意外发现 nil)

ios - 从 uiviewcontroller 操作 uicollectionviewcell 内的 uicollectionview

mysql - 创建包含多个表的 View ?

view - 从 POSTGRESQL DB 获取原始 View 定义

ios - 创建 NAT64 网络

iOS - 在另一个 Xcode 项目中包含静态库项目... header 问题

iphone - 在 ios 应用程序中存储 .png 图像?

ruby-on-rails - 从 View 中输入 Rails

ios BLE 在后台模式下被杀死后数据丢失