【问题标题】:How to apply different corner radius to different corners of UIView in iOS Swift?如何在 iOS Swift 中将不同的角半径应用于 UIView 的不同角?
【发布时间】:2018-05-14 08:19:44
【问题描述】:

我想使用swiftiOSUIView 的不同角应用不同的角半径值。这意味着UIViewtopLeft, topRight, bottomLeft, bottomRight 角应该有不同 corner radiuous(即1、5、10、15)。怎么实现呢?

我尝试以相同的方式应用cornerRadius,但它会覆盖以前的cornerRadius 值。

【问题讨论】:

标签: ios swift uiview cornerradius


【解决方案1】:

如果您的部署目标高于 iOS 10,那么您应该使用 ma​​skedCorners

if #available(iOS 11.0, *) {
    customeView.clipsToBounds = true
    customeView.layer.cornerRadius = 5
    customeView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMinYCorner]
} 

试试这个

let path = UIBezierPath(roundedRect: customeView.bounds, byRoundingCorners:[.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 5, height: 5))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            customeView.layer.mask = mask

【讨论】:

  • 这不起作用,因为它只允许将角四舍五入到一个值或根本不四舍五入。最初的海报希望不同的角圆成不同的度数。
猜你喜欢
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2022-10-17
  • 2019-12-10
  • 2016-12-03
相关资源
最近更新 更多