Skip to content

Instantly share code, notes, and snippets.

@arjun011
Last active September 8, 2021 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arjun011/e8644b72d01691b56f80d31fca0dc39b to your computer and use it in GitHub Desktop.
Save arjun011/e8644b72d01691b56f80d31fca0dc39b to your computer and use it in GitHub Desktop.
Apply round and shadow effect on UIView
import Foundation
import UIKit
@IBDesignable class RoundShadowView: UIView {
@IBInspectable var shadow: Bool {
get {
return layer.shadowOpacity > 0.0
}
set {
if newValue == true {
self.addShadow()
}
}
}
@IBInspectable var cornerRadius: CGFloat {
get {
return self.layer.cornerRadius
}
set {
self.layer.cornerRadius = newValue
// Don't touch the masksToBound property if a shadow is needed in addition to the cornerRadius
if shadow == false {
self.layer.masksToBounds = true
}
}
}
func addShadow(shadowColor: CGColor = UIColor.black.cgColor,
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0),
shadowOpacity: Float = 0.7,
shadowRadius: CGFloat = 3.0) {
layer.shadowColor = shadowColor
layer.shadowOffset = shadowOffset
layer.shadowOpacity = shadowOpacity
layer.shadowRadius = shadowRadius
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment