如何在Swift 4.2中为UITableViewCell的自定义视图设置阴影?

时间:2018-11-26 12:34:51

标签: ios swift layer shadow

我已经尝试过了,但是对我没有用:

cell.totalView?.layer.shadowColor = UIColor.yellow.cgColor
cell.totalView?.layer.shadowOffset = CGSize.zero
cell.totalView?.layer.shadowOpacity = 1.0
cell.totalView?.layer.shadowRadius = 10
cell.totalView?.layer.shouldRasterize = true

4 个答案:

答案 0 :(得分:1)

尝试通过自定义单元格的awakeFromNib方法为自定义视图添加阴影。

例如

在您的customTableViewCell类中添加以下方法

func dropShadow(view: UIView, shadowOpacity: Float = 0.2, shadowColor: CGColor = UIColor.gray.cgColor, shadowRadius: CGFloat = 5){
    view.layer.shadowColor = shadowColor
    view.layer.shadowOpacity = shadowOpacity
    view.layer.shadowOffset = CGSize.zero
    view.layer.shadowRadius = shadowRadius
}

并通过awakeFromNib之类的方法进行调用

override func awakeFromNib() {
    super.awakeFromNib()

    dropShadow(view: yourCustomView, shadowOpacity: 0.2, shadowColor: UIColor.yellow.cgColor, shadowRadius: 5)        
}

答案 1 :(得分:0)

在CustomViewCell中制作insideView并将此函数加载到awakeFromNib()

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

N = 200
k = 2*np.pi/0.001
a = -0.01
b = 0.01
z = 20


f = lambda y_prime, x_prime: 2*(k/(2*np.pi*z))*np.exp(((1j*k)/(2*z))*((x-x_prime)**2+(y-y_prime)**2))


x = y = np.linspace(-5, 5, N)
#x, y = np.meshgrid(x, y)
zvals = integrate.dblquad(f, a, b, lambda x: a, lambda x: b)
plt.xticks([])
plt.yticks([])
plt.imshow(zvals)
plt.show()

答案 2 :(得分:0)

将masksToBounds设置为true

  cell.totalView?.layer.masksToBounds = true

答案 3 :(得分:0)

只需在表格单元格中添加一个UIVIew,所有边距为8。

创建一个名称为CardView.swift的文件

import UIKit

@IBDesignable
class CardView: UIView {

    @IBInspectable var cornerRadius: CGFloat = 2

    @IBInspectable var shadowOffsetWidth: Int = 0
    @IBInspectable var shadowOffsetHeight: Int = 1
    @IBInspectable var shadowColor: UIColor? = UIColor.black
    @IBInspectable var shadowOpacity: Float = 0.1

    override func layoutSubviews() {

        layer.cornerRadius = cornerRadius
        let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        layer.masksToBounds = false
        layer.shadowColor = shadowColor?.cgColor
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
        layer.shadowOpacity = shadowOpacity
        layer.shadowPath = shadowPath.cgPath
    }
}

enter image description here 只需在情节提要中添加自定义类