首页 > 解决方案 > 两个 UIButton 形成一个具有渐变颜色,另一个具有边框和圆角

问题描述

在此处输入图像描述

我试图在[.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)

这是我的输出

在此处输入图像描述

请帮忙。

标签: iosswiftuibuttongradient

解决方案


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

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

在此处输入图像描述


推荐阅读