Skip to content

Instantly share code, notes, and snippets.

@aibobrov
Created September 17, 2018 13:59
Show Gist options
  • Save aibobrov/c689ec498fa0bdedab82bd635ba44a18 to your computer and use it in GitHub Desktop.
Save aibobrov/c689ec498fa0bdedab82bd635ba44a18 to your computer and use it in GitHub Desktop.
IBDesignable subclass of UIImageView
import UIKit
@IBDesignable
class ImageView: UIImageView {
// MARK: - Layer stuff
@IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
}
}
// MARK: - IBInspectable border
@IBInspectable
var borderColor: UIColor? {
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
set {
layer.borderColor = newValue?.cgColor
}
}
@IBInspectable
var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
// MARK: - IBInspectable shadow
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
@IBInspectable
var shadowColor: UIColor? {
get {
guard let color = layer.shadowColor else { return nil }
return UIColor(cgColor: color)
}
set {
layer.shadowColor = newValue?.cgColor
}
}
@IBInspectable
var imageTintColor: UIColor! {
get {
return tintColor
}
set {
tintColor = newValue
}
}
public func clear() {
layer.cornerRadius = 0
layer.borderColor = nil
layer.borderWidth = 0
layer.shadowRadius = 0
layer.shadowOpacity = 0
layer.shadowOffset = .zero
layer.shadowColor = nil
}
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.cornerRadius = cornerRadius
layer.borderColor = borderColor?.cgColor
layer.borderWidth = borderWidth
layer.shadowRadius = shadowRadius
layer.shadowOpacity = shadowOpacity
layer.shadowOffset = shadowOffset
layer.shadowColor = shadowColor?.cgColor
tintColor = imageTintColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment