Dima L
Dima L

Reputation: 70

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.

I have the next code for it in separate file 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 "Attribute inspector" of 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

Does someone know why it doesn't work for UIView ?

Any help greatly appreciated.

Thank you.

Upvotes: 2

Views: 1361

Answers (2)

Azhar Ali
Azhar Ali

Reputation: 299

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

or in your storyboard like so:

enter image description here

Upvotes: 3

Madhuri Agrawal
Madhuri Agrawal

Reputation: 66

Please Update the function

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

Upvotes: 0

Related Questions