Created
February 9, 2017 17:03
-
-
Save jzucker2/8ab6a9f13507feef269584c150167665 to your computer and use it in GitHub Desktop.
Beginnings of useful Swift snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift | |
public extension UIImage { | |
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) { | |
let rect = CGRect(origin: .zero, size: size) | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) | |
color.setFill() | |
UIRectFill(rect) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
guard let cgImage = image?.cgImage else { return nil } | |
self.init(cgImage: cgImage) | |
} | |
} | |
extension UIView { | |
static func reuseIdentifier() -> String { | |
return NSStringFromClass(self) | |
} | |
} | |
extension UIView { | |
var hasConstraints: Bool { | |
let hasHorizontalConstraints = !self.constraintsAffectingLayout(for: .horizontal).isEmpty | |
let hasVerticalConstraints = !self.constraintsAffectingLayout(for: .vertical).isEmpty | |
return hasHorizontalConstraints || hasVerticalConstraints | |
} | |
func forceAutoLayout() { | |
self.translatesAutoresizingMaskIntoConstraints = false | |
} | |
func center(in view: UIView) { | |
forceAutoLayout() | |
centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true | |
} | |
func size(to view: UIView) { | |
forceAutoLayout() | |
widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true | |
heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true | |
} | |
func sizeAndCenter(to view: UIView) { | |
size(to: view) | |
center(in: view) | |
} | |
} | |
extension UINavigationItem { | |
func setPrompt(with message: String, for duration: Double = 3.0) { | |
assert(duration > 0.0) | |
assert(duration < 10.0) | |
self.prompt = message | |
DispatchQueue.main.asyncAfter(deadline: .now() + duration) { | |
self.prompt = nil | |
} | |
} | |
} | |
class ImageToDataTransformer: ValueTransformer { | |
class override func allowsReverseTransformation() -> Bool { | |
return true | |
} | |
class override func transformedValueClass() -> Swift.AnyClass { | |
return NSData.self | |
} | |
override func transformedValue(_ value: Any?) -> Any? { | |
guard let image = value as? UIImage else { | |
fatalError("can only handle UIImage, not: \(value.debugDescription)") | |
} | |
let data = UIImageJPEGRepresentation(image, 1.0) | |
return data | |
} | |
override func reverseTransformedValue(_ value: Any?) -> Any? { | |
guard let data = value as? Data else { | |
fatalError("can only handle Data, not: \(value.debugDescription)") | |
} | |
let image = UIImage(data: data) | |
return image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment