CALayer border 1px bug

问题描述 投票:2回答:1

我创建一个按钮

let roundedButton = UIButton(type: .system)
roundedButton.frame = .init(x: 100, y: 100, width: 100, height: 100)
roundedButton.backgroundColor = UIColor.black
roundedButton.layer.borderColor = UIColor.white.cgColor
roundedButton.layer.borderWidth = 30
roundedButton.layer.cornerRadius = roundedButton.frame.size.width/2
roundedButton.layer.masksToBounds = true

并且期望得到带有白色边框的黑色按钮。但是,可以看到一个奇怪的边界。是什么原因造成的?那是个错误吗?以及如何解决?

enter image description here

ios swift calayer
1个回答
0
投票
    view.backgroundColor = .white

    let roundedButton = UIButton(type: .system)
    roundedButton.frame = .init(x: 100, y: 100, width: 100, height: 100)
    roundedButton.backgroundColor = .clear
    let _border = CAShapeLayer()
    _border.path = UIBezierPath(roundedRect: roundedButton.bounds, cornerRadius:roundedButton.frame.size.width/2).cgPath
    _border.frame = roundedButton.bounds
    _border.strokeColor = UIColor.white.cgColor
    _border.fillColor = UIColor.black.cgColor
    _border.backgroundColor = UIColor.white.cgColor
    _border.lineWidth = 30.0
    _border.masksToBounds = true
    roundedButton.layer.addSublayer(_border)

    view.addSubview(roundedButton)

关于SO的相关问题:iOS UIButton with rounded corners and background bug

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.