Skip to content

Instantly share code, notes, and snippets.

View eleev's full-sized avatar

Astemir Eleev eleev

View GitHub Profile
@eleev
eleev / UIImage+SolidColor.swift
Created March 24, 2017 11:18
Extension for creating UIImage from a UIColor. Swift 3
extension UIImage {
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 }
@eleev
eleev / Date+String.swift
Last active March 24, 2017 11:19
Extension for Date class that provides easy-to-use feature for convering date to string. Swift 3
extension Date {
struct Formatter {
static let shortDate = DateFormatter(dateStyle: .short)
static let longDate = DateFormatter(dateStyle: .long)
static let fullDate = DateFormatter(dateStyle: .full)
}
var shortDate: String { return Formatter.shortDate.string(from: self) }
var longDate: String { return Formatter.longDate.string(from: self) }
var fullDate: String { return Formatter.fullDate.string(from: self) }
@eleev
eleev / UIScreen+InterfaceOrientation.swift
Last active March 24, 2017 11:19
This extnesion allows to get UIInterfaceOrientation from UIScreen. It is useful for cases when by some reasons UIStatusBar and UIDevice.current.orientation are not available e.g. iMessage extension target in iOS 10.
extension UIScreen {
var orientation: UIInterfaceOrientation {
let point = coordinateSpace.convert(CGPoint.zero, to: fixedCoordinateSpace)
if point == CGPoint.zero {
return .portrait
} else if point.x != 0 && point.y != 0 {
return .portraitUpsideDown
} else if point.x == 0 && point.y != 0 {
return .landscapeLeft
@eleev
eleev / Auto-layout-keyboard-adjustment.md
Created March 17, 2017 09:08 — forked from dlo/Auto-layout-keyboard-adjustment.md
How to adjust a view's height with Auto Layout when a keyboard appears or disappears in iOS 7.

This gist outlines how to resize a view when a keyboard appears using Auto Layout (there are a bunch of code samples out there that manually adjust the view's frame, but that's just so 2013). The method I outline below works universally on both iPhone and iPad, portrait and landscape, and is pretty darn simple.

Setting Up

The first thing to do is to define our containing view controller, the view, and the bottom constraint that we'll use to adjust its size.

Here's HeightAdjustingViewController.h. We don't need to expose any public properties, so it's pretty bare.

@eleev
eleev / URLForPHAsset.swift
Last active February 17, 2024 23:56
Getting URL for PHAsset (Swift 3.0)
func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})