Set Background Gradient on Button in Swift

前端 未结 10 1207
礼貌的吻别
礼貌的吻别 2020-12-12 21:54

I have no idea how to set the background gradient on a button (without making the background gradient an image). This is so different from Android.

Here\'s a class I

10条回答
  •  無奈伤痛
    2020-12-12 22:23

    I have tried all of them this is my button init inside of viewdidload

    let button = UIButton()
        button.setTitle("Alper", for: .normal)
        button.layer.borderColor = UIColor.white.cgColor
        button.layer.borderWidth = 1
        view.addSubview(button)
        button.anchor(top: nil, left: nil, bottom: logo.topAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, height: 50, width: 100)
        let gradientx = CAGradientLayer()
        gradientx.colors = [UIColor.blue,UIColor.red]
        gradientx.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradientx.endPoint = CGPoint(x: 1.0, y: 1.0)
        gradientx.frame = button.bounds
        button.layer.insertSublayer(gradientx, at: 0)
    

    anchor is an extension, so this is irrelevant gradient.

提交回复
热议问题