Два UIButton образуют один, имеющий градиентный цвет, а другой имеющий границу и закругленный угол - PullRequest
0 голосов
/ 23 мая 2018

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 голосов
/ 23 мая 2018

Прадип, лучше добавить кнопки в UIView.Вам будет легко управлять.Я сделал UI так, как вы хотите, используя UIView.Смотрите,

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

...