如果我打开了显示和亮度设置中的粗体功能,则意味着CAGradientLayer
不适用于图像。
let btn = UIButton(type: .custom)
btn.frame = CGRect(x: 100, y: 100, width: #imageLiteral(resourceName: "White_Normal").size.width, height: #imageLiteral(resourceName: "White_Normal").size.height)
btn.setBackgroundImage(#imageLiteral(resourceName: "White_Normal").withRenderingMode(.alwaysTemplate), for: UIControlState.normal)
btn.setBackgroundImage(#imageLiteral(resourceName: "White_Active").withRenderingMode(.alwaysTemplate), for: UIControlState.highlighted)
btn.tintColor = UIColor.purple
let gradientLayer = CAGradientLayer()
gradientLayer.frame = btn.layer.bounds
gradientLayer.colors = [UIColor(white: 1.0, alpha: 1.0).cgColor, UIColor(white: 0.0, alpha: 1.0).cgColor]
gradientLayer.locations = [0.0, 0.5]
gradientLayer.cornerRadius = btn.layer.cornerRadius
btn.layer.mask = gradientLayer
print()
view.addSubview(btn)
let imageView = UIImageView(image: btn.backgroundImage(for: .normal))
imageView.frame = CGRect(x: 100, y: #imageLiteral(resourceName: "White_Normal").size.height+100, width: #imageLiteral(resourceName: "White_Normal").size.width, height: #imageLiteral(resourceName: "White_Normal").size.height)
view.addSubview(imageView)