简体   繁体   English

UIButton 渐变背景在 swift 4 中不起作用

[英]UIButton Gradient background is not working in swift 4

this is my code for a uibutton which isnt working.Im doing autolayout programatically,have tried many solution on the internet but its not working.Any suggestions?这是我的 uibutton 代码,它不起作用。我正在以编程方式进行自动布局,在互联网上尝试了许多解决方案,但都不起作用。有什么建议吗?

let ButtonSignUp: UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = AppThemeFonts.buttonFonts
        btn.layer.cornerRadius = 6
        btn.setTitle("SIGNUP", for: .normal)
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = btn.bounds
        gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
        btn.layer.insertSublayer(gradientLayer, at: 0)
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
        gradientLayer.locations = [0.0, 1.0]
        return btn
    }()

following is how im setting constraint in viewdidload以下是我如何在 viewdidload 中设置约束

let buttonsView: UIStackView = {
                let view = UIStackView()
                view.distribution = .fillEqually
                view.axis = .horizontal
                view.spacing = 10
                view.addArrangedSubview(ButtonSignUp)
                view.addArrangedSubview(ButtonLogin)
                view.translatesAutoresizingMaskIntoConstraints = false
                return view
            }()

            self.view.addSubview(buttonsView)

            let xConstraintButtonView = NSLayoutConstraint(item: buttonsView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
            let yConstraintButtonView = NSLayoutConstraint(item: buttonsView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.75, constant: 0)
            let widthConstraintButtonView = NSLayoutConstraint(item: buttonsView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.9, constant: 0)
            let heightConstraintButtonView = NSLayoutConstraint(item: buttonsView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.068, constant: 0)

        self.view.addConstraints([xConstraintButtonView,yConstraintButtonView,widthConstraintButtonView,heightConstraintButtonView])
  • You need to set btn width and height.您需要设置 btn 的宽度和高度。
  • btn.layer.insertSublayer(gradientLayer, at: 0) call before return btn. btn.layer.insertSublayer(gradientLayer, at: 0) 在返回 btn 之前调用。

your code like this :-你的代码是这样的:-

let ButtonSignUp: UIButton = {
                    let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 50))
                    btn.translatesAutoresizingMaskIntoConstraints = false
                    btn.setTitleColor(.white, for: .normal)
                    btn.titleLabel?.font = AppThemeFonts.buttonFonts
                    btn.layer.cornerRadius = 6
                    btn.setTitle("SIGNUP", for: .normal)
                    let gradientLayer = CAGradientLayer()
                    gradientLayer.frame = btn.bounds
                    gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]

                    gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
                    gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
                    gradientLayer.locations = [0.0, 1.0]
                    btn.layer.insertSublayer(gradientLayer, at: 0)
                    return btn
                }()

You need override layoutSubviews() function before set gradient for button.在为按钮设置渐变之前,您需要覆盖layoutSubviews()函数。
Inside class SomeButton: UIButton {} add bellow code:class SomeButton: UIButton {}添加以下代码:

override open func layoutSubviews() {
    super.layoutSubviews()
    gradient?.frame = self.layer.bounds
}

end then set gradient color like:结束然后设置渐变颜色,如:

func customGradient() {
    gradient?.removeFromSuperlayer()

    gradient = CAGradientLayer()
    guard let gradient = gradient else { return }

    gradient.frame = self.layer.bounds
    gradient.colors = [gradientStartColor.cgColor, gradientEndColor.cgColor]
    gradient.startPoint = CGPoint(x: 0, y: 0)
    gradient.endPoint = CGPoint(x: 1, y: 0)
    gradient.cornerRadius = self.cornerRadius
    self.layer.insertSublayer(gradient, below: self.imageView?.layer)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM