【问题标题】:Only top left corner is rounded instead of topLeft and bottomLeft只有左上角是圆角的,而不是 topLeft 和 bottomLeft
【发布时间】:2017-04-29 21:36:58
【问题描述】:

我正在尝试使 UIView 的 topLeft 和 bottomLeft 角变圆,但是由于某种原因,只有 topLeft 具有边框半径。这是什么原因造成的?

这就是我正在做的事情:

我有扩展:

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

我将它应用于这样的视图:

myView.roundCorners([.topLeft, .bottomLeft], radius: 10.0)

我在单元格内使用它,我也在使用自动布局。

目前它看起来像这样(绿色是我正在谈论的视图)但我希望 bottomLeft 也有角半径:

【问题讨论】:

  • 几周前我遇到了类似的问题。我能够通过增加视图的宽度/高度来解决它。不知道为什么...

标签: ios swift uiview uibezierpath


【解决方案1】:

问题在于你的时机。您的扩展依赖于self.bounds。但是如果你过早调用这个扩展,比如viewDidLoad,那些bounds还没有确定。您的视图由自动布局调整大小;这意味着在调用视图的 layoutSubviews 或视图控制器的 viewDidLayoutSubviews 之前,您没有边界。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2012-02-17
    • 2021-05-25
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多