Skip to content

Instantly share code, notes, and snippets.

@longlongjump
Created October 17, 2018 14:05
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 longlongjump/5019a85681f9f94b6e185f3ba9b4fbe1 to your computer and use it in GitHub Desktop.
Save longlongjump/5019a85681f9f94b6e185f3ba9b4fbe1 to your computer and use it in GitHub Desktop.
//
// UIView+Utils.swift
// void
//
// Created by Eugen Ovchynnykov on 1/8/16.
//
import UIKit
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
}
get {
return layer.cornerRadius
}
}
@IBInspectable var shadowColor: UIColor? {
set {
layer.shadowColor = newValue?.cgColor
}
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
}
@IBInspectable var shadowRadius: CGFloat {
set {
layer.shadowRadius = newValue
}
get {
return layer.shadowRadius
}
}
@IBInspectable var shadowOpacity: CGFloat {
set {
layer.shadowOpacity = Float(newValue)
}
get {
return CGFloat(layer.shadowOpacity)
}
}
@IBInspectable var shadowOffset: CGPoint {
set {
layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
}
get {
return CGPoint(x: layer.shadowOffset.width, y: layer.shadowOffset.height)
}
}
@IBInspectable var borderColor: UIColor? {
set {
self.layer.borderColor = newValue?.cgColor
}
get {
if let color = self.layer.borderColor {
return UIColor(cgColor: color)
}
return nil
}
}
@IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var useSystemBorderWidth: Bool {
set {
layer.borderWidth = newValue ? 1.0 / UIScreen.main.scale : 0
}
get {
return layer.borderWidth == 1.0 / UIScreen.main.scale
}
}
}
extension UIView {
func parentViewOfClass<T>(_ type: T.Type) -> T? {
if let view = superview as? T {
return view
}
return superview?.parentViewOfClass(type)
}
func childViewOfClass<T>(_ type: T.Type) -> T? {
for subview in subviews {
if let view = subview as? T {
return view
} else if let view = subview.childViewOfClass(type) {
return view
}
}
return nil
}
var firstResponder: UIView? {
for subview in subviews {
if subview.isFirstResponder {
return subview
} else if let view = subview.firstResponder {
return view
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment