【问题标题】:Xcode UIView has no members like border color and etcXcode UIView 没有边框颜色等成员
【发布时间】:2017-03-14 10:17:52
【问题描述】:

截图: 如果我在代码中使用它,则会收到此消息: 喜欢:“UIView”类型的值没有成员cornerRadius 怎么了?昨天我没有这个问题

【问题讨论】:

    标签: ios objective-c swift xcode uiview


    【解决方案1】:

    您需要使用 Button 的图层来设置边框属性。例如:

    button.layer.cornerRadius = button.frame.height / 2.0
    button.layer.masksToBounds = true
    button.layer.borderColor = UIColor.lightGray.cgColor
    button.layer.borderWidth = 1.0
    

    添加此扩展以在情节提要中获取这些选项。

        extension UIView {
        @IBInspectable var cornerRadius: CGFloat {
            get {
                return layer.cornerRadius
            }
            set {
                layer.cornerRadius = newValue
                layer.masksToBounds = newValue > 0
            }
        }
        @IBInspectable var borderWidth: CGFloat {
            get {
                return layer.borderWidth
            }
            set {
                layer.borderWidth = newValue
            }
        }
        @IBInspectable var borderColor: UIColor? {
            get {
                let color = UIColor.init(cgColor: layer.borderColor!)
                return color
            }
            set {
                layer.borderColor = newValue?.cgColor
            }
        }
        @IBInspectable var shadowRadius: CGFloat {
            get {
                return layer.shadowRadius
            }
            set {
                layer.shadowColor = UIColor.black.cgColor
                layer.shadowOffset = CGSize(width: 0, height: 2)
                layer.shadowOpacity = 0.4
                layer.shadowRadius = shadowRadius
            }
        }
    }
    

    【讨论】:

    • 是的,没错,有一个错误。但是为什么我在 Storyboard 中没有这个选项?
    • 默认情况下,这些选项在情节提要中不可编辑。如果您愿意,可以添加 UIView extension 以在情节提要中获取这些选项。检查更新的答案。
    • 谢谢!!!我使用了库“Material”,默认情况下它有这个,昨天我删除了这个库,它发生了)我认为这是 ios 中的默认设置(我是 ios 编程新手)
    【解决方案2】:

    您不能将 CALayer 属性直接设置为 UIElement。相反,需要使用 .layer 属性。

    例子:

    button.layer.cornerRadius = button.frame.height/ 2.0
    
    button.layer.maskToBounds = true
    

    【讨论】:

      【解决方案3】:

      应该是这样的

      #import <QuartzCore/QuartzCore.h>
      
      [[myButton layer] setBorderWidth:2.0f];
      
      [[myButton layer] setBorderColor:[UIColor greenColor].CGColor];
      
      myButton.layer.cornerRadius = myButton.frame.height / 2.0;
      

      供参考:

      https://stackoverflow.com/a/32468101/5184217

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 2012-12-28
        • 1970-01-01
        相关资源
        最近更新 更多