尝试将渐变颜色设置为以编程方式编写的View Controller上的按钮。但它不会出现!
我尝试添加具有指定索引的子层或在viewdidload闭包中添加渐变子层,仍然没有任何效果!
private let getStartButton : UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("GET STARTED", for: .normal)
button.addTarget(self, action: #selector(getStarted), for: .touchUpInside)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.layer.cornerRadius = 5
button.clipsToBounds = true
button.isHidden = false
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.lightGreyBlue.cgColor, UIColor.sea.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = button.bounds
button.layer.addSublayer(gradientLayer)
return button
}()
fileprivate func setupBottomControls() {
view.addSubview(getStartButton)
getStartButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -12).isActive = true
getStartButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12).isActive = true
getStartButton.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width-32
).isActive = true
getStartButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
它仅显示按钮的标题。
答案 0 :(得分:0)
你在说
gradientLayer.frame = button.bounds
但是按钮没有边界。因此该图层没有框架。
在viewDidLayoutSubviews
之后,在自动布局约束生效之前,按钮将没有任何大小。