简体   繁体   English

Swift @ IBDesignable / @ IBInspectable UIView样式

[英]Swift @IBDesignable/@IBInspectable UIView styling

I'm trying to style my UIView inside table cell prototype to have it shadow for it and a couple more styling customization but for some reason when I build the project the changes are not applied. 我正在尝试对表格单元格原型中的UIView进行样式设置,使其具有阴影以及更多样式自定义,但是由于某些原因,在构建项目时未应用更改。

I have the next code for it in separate file CustomView.swift 我在单独的文件CustomView.swift有下一个代码

import UIKit

@IBDesignable
class CustomView: UIView {



    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }

    @IBInspectable var shadowOpacity: Float = 0 {
        didSet {
            layer.shadowOpacity = shadowOpacity
        }
    }

    @IBInspectable var shadowRadius: CGFloat = 0 {
        didSet {
            layer.shadowRadius = shadowRadius
        }
    }

    @IBInspectable var shadowOffset: CGSize = CGSize(width: 0.0, height: 0.0) {
        didSet {
            layer.shadowOffset = shadowOffset
        }
    }

    @IBInspectable var shadowColor: UIColor? = UIColor(red: 157/255, green: 157/255, blue: 157/255, alpha: 1.0) {
        didSet {
            layer.shadowColor = shadowColor?.cgColor
        }
    }

}

I see the field inside the "Attribute inspector" of Xcode 9 我看到Xcode 9的“属性检查器”中的字段 Xcode 9的“属性检查器”

but after building it the change are not applied basically I don't see the shadow for view. 但是构建它之后,基本上没有应用更改,我看不到阴影。

The most interesting that I'm using the same approach to customize text fields as well as buttons and this code works for UIButton/UITextField but not for UIView 最有趣的是,我使用相同的方法来自定义文本字段和按钮,并且此代码适用于UIButton/UITextField但不适用于UIView

Does someone know why it doesn't work for UIView ? 有人知道为什么它不适用于UIView吗?

Any help greatly appreciated. 任何帮助,不胜感激。

Thank you. 谢谢。

check your code: 检查您的代码:

layer.masksToBounds = cornerRadius > 0

Change that to: 更改为:

layer.masksToBounds = false

And also make sure that your clipsToBounds is set to false either in code like so: 并且还要确保您的clipsToBounds在以下代码中都设置为false

clipsToBounds = false

or in your storyboard like so: 或像这样在情节提要中:

在此处输入图片说明

Please Update the function 请更新功能

@IBInspectable
    var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM