ios - 两个 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)
这是我的输出
请帮忙。
解决方案
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
上述代码的输出将如下所示。
推荐阅读
- python - 如何根据 key.is_pressed 语句让我的程序播放声音?
- string - 函数和字符串数组
- java - Eclipse 项目中的 MojoFailureException
- java - 如何将数据保存到 Spring Data JPA 中的新行中?
- windows - 更改 Anaconda 的命令行提示符
- laravel - Laravel:Web Api,如何判断用户(应用程序)是否在线?
- typescript - 这个类型定义可以统一吗?
- angular - 更新一个字段时,angular 7 ngrx 触发订阅所有字段
- python - 如何从 PyPI 获取最新的 sdist 链接
- python - 如何将熊猫数据框信息存储在 csv 文件中