两个UIButton形成一个具有渐变颜色,另一个具有边框和圆角

时间:2018-05-23 08:27:32

标签: ios swift uibutton gradient

enter image description here

我试图在 [.topLeft,.bottomLeft] [.topRight,.bottomRight] 上使用渐变颜色和圆角半径来实现此目的但我不能在一边实现边框颜色。

这是我的代码

//btnMale gradient color
btnMale.roundCorners([.topLeft,.bottomLeft], radius: 20)
btnFemale.roundCorners([.topRight,.bottomRight], radius: 20)
btnMale.backgroundColor = .clear
let gradient1: CAGradientLayer = CAGradientLayer()
gradient1.frame = CGRect(x: 0, y: 0, width: (self.view.frame.size.width - 90)/2 , height: btnMale.frame.size.height)
gradient1.colors = [Colors.appYellow,Colors.appRed].map { $0.cgColor }
gradient1.startPoint = GradientOrientation.horizontal.startPoint
gradient1.endPoint = GradientOrientation.horizontal.endPoint
btnMale.layer.insertSublayer(gradient1, at: 0)
btnMale.applyGradient(withColours: [Colors.appYellow,Colors.appRed], gradientOrientation: .horizontal)
btnMale.borderWidth = 0.0

btnFemale.borderWidth = 0.5
btnFemale.borderColor = .black
btnMale.setImage(UIImage(named: Images.imgMaleIconWhite), for: .normal)
btnFemale.setImage(UIImage(named: Images.imgFemaleIconBlack), for: .normal)

这是我的输出

enter image description here

请帮忙。

1 个答案:

答案 0 :(得分:0)

Pradip最好在UIView中添加按钮。您可以轻松管理。我使用UIView制作了你想要的UI。见这个,

let button = UIButton.init(frame: CGRect.init(x: 0, y: 250, width: 200, height: 100))
    button.setTitle("HI", for: .normal)
    button.addTarget(self, action: #selector(self.btnTapped(pageNo:)), for: .touchUpInside)
    self.view.addSubview(button)

    let view = UIView.init(frame: CGRect.init(x: 50, y: 250, width: 250, height: 50))
    view.backgroundColor = UIColor.red
    self.view.addSubview(view)

    let btnMale = UIButton.init(type: .custom)
    btnMale.frame = CGRect.init(x: 0, y: 0, width: 125, height: 50)
    btnMale.setTitle("Male", for: .normal)

    btnMale.applyGradient(colours: [UIColor.yellow,UIColor.red])


    view.addSubview(btnMale)

    let btnFemale = UIButton.init(type: .custom)
    btnFemale.frame = CGRect.init(x: 125, y: 0, width: 125, height: 50)
    btnFemale.setTitle("Female", for: .normal)
    view.addSubview(btnFemale)

    view.layer.cornerRadius = 25
    view.clipsToBounds = true

上述代码的输出将如下所示。

enter image description here