【问题标题】:Value of type 'UIView?' has no member 'roundCorners''UIView' 类型的值?没有成员'roundCorners'
【发布时间】:2023-03-09 16:00:01
【问题描述】:

'UIView?' 类型的值没有成员'roundCorners'

cardView = UIView()
cardView.backgroundColor = Constants.Colors.colorOptimusThree
cardView.roundCorners(cornerRadius: 4.0)
view.addSubview(cardView)

【问题讨论】:

  • UIView 没有名为“roundCorners”的方法。
  • 这可能是您在某处看到的扩展程序。

标签: ios swift


【解决方案1】:

UIView 有一个名为layer 的属性,它有cornerRadius 可以使用,所以你必须使用cardView.layer.cornerRadius = "your float value"

【讨论】:

    【解决方案2】:

    试试这个..

     cardView.layer.cornerRadius = 10.0
     cardView.layer.borderWidth = 0.5
     cardView.clipsToBounds = true
    

    【讨论】:

      【解决方案3】:

      试试

      cardView.layer.cornerRadius = 4.0
      

      【讨论】:

        【解决方案4】:

        您可以使用扩展程序或就在您的任何视图下方。

        下视图

         cardView.layer.cornerRadius = 5
         cardView.layer.borderWidth = 2
         cardView.layer.maskToBounds = true
        

        扩展

        extension UIView {
            func cornerRadius(_ radius: CGFloat, borderWidth: CGFloat = 1) {
                layer.cornerRadius = radius
                layer.borderWidth = borderWidth
                layer.masksToBounds = true
            }
        }
        

        你可以通过这样的调用来使用扩展方法

        cardView.cornerRadius(5, borderWidth: 1)
        

        您将看到 2 种方法。第一个没有borderWidth,另一个有borderWidth。 如果你想宽度为 1,那么你可以忽略第二个参数。

        【讨论】:

          【解决方案5】:
          extension UIView {
              func makeRoundedCorners(cornerRadius: CGFloat, borderWidth: CGFloat, masksToBounds: Bool) {
                  layer.cornerRadius = cornerRadius
                  layer.borderWidth = borderWidth
                  layer.masksToBounds = masksToBounds
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多