Skip to content

Instantly share code, notes, and snippets.

@rsaenzi
Last active October 4, 2017 01:49
Show Gist options
  • Save rsaenzi/1a3a7d62d2432917f0922b7d60d288ec to your computer and use it in GitHub Desktop.
Save rsaenzi/1a3a7d62d2432917f0922b7d60d288ec to your computer and use it in GitHub Desktop.
To add inspectable properties to control the border of a view
//
// UIView+Border.swift
//
// Created by Rigoberto Sáenz Imbacuán (https://www.linkedin.com/in/rsaenzi/)
// Copyright © 2017. All rights reserved.
//
import UIKit
@IBDesignable
extension UIView {
@IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
clipsToBounds = newValue > 0
}
}
@IBInspectable
var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable
var borderColor: UIColor? {
set {
layer.borderColor = newValue?.cgColor
}
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
} else {
return nil
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment