Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View eleev's full-sized avatar

Astemir Eleev eleev

View GitHub Profile
@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 / 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 / UIApplication+InternetAvailability.swift
Created March 24, 2017 11:22
Class-level extension for UIApplication for checking internet connectivity. Swift 3
extension UIApplication {
class func isInternetAvailable() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
@eleev
eleev / UIImage+LandscapeCameraCaptureOrientationFix.swift
Created March 24, 2017 12:57
This extension for UIImage fixes image orientation for cases when the iamge was captured using AVFoundation in landscape interface orientation. Swift 3
func landscapeCameraCaptureOrientationFix(for interfaceOrinetation: UIInterfaceOrientation) -> UIImage? {
var imageOrientation: UIImageOrientation!
var shouldOrient: Bool = false
if interfaceOrinetation == .landscapeRight {
imageOrientation = UIImageOrientation.left
shouldOrient = !shouldOrient
} else if interfaceOrinetation == .landscapeLeft {
imageOrientation = UIImageOrientation.right
@eleev
eleev / UIImage+TiffOrientationFix.swift
Created March 24, 2017 13:08
This calss-level UIImage extension aims to help identifying raw value for UIImageOrientation. This may be useful when warking with CGImage or CIImage classes. Swift 3
extension UIImage {
class func imageOrientationToTiffOrientation(_ value: UIImageOrientation) -> Int32 {
switch (value) {
case .up:
return 1
case .down:
return 3
case .left:
return 8
case .right:
@eleev
eleev / UIImage+Resize.swift
Created March 26, 2017 11:44
Class-level extension for UIImage that allow to resize input image based on expected image width or/and height. Swift 3
extension UIImage {
class func resize(_ image: UIImage, newWidth: CGFloat) -> UIImage? {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
@eleev
eleev / DispatchQueue+DispatchOnce.swift
Last active March 30, 2018 14:13
DispatchOnce + Swift 3 (and above).swift
public extension DispatchQueue {
// MARK: - Properties
private static var _onceTracker = [String]()
// MARK: - Methods
/// Executes a block of code, associated with a unique token, only once. The code is thread safe and will onle execute the code once even in the presence of multithreaded calls.
@eleev
eleev / KeyCodesMacOS.swift
Created July 29, 2018 15:54
The code snippet contains key codes for `macOS` keyboard
import Carbon
struct KeyCode {
static let a = UInt16(kVK_ANSI_A)
static let b = UInt16(kVK_ANSI_B)
static let c = UInt16(kVK_ANSI_C)
static let d = UInt16(kVK_ANSI_D)
static let e = UInt16(kVK_ANSI_E)
static let f = UInt16(kVK_ANSI_F)
static let g = UInt16(kVK_ANSI_G)
@eleev
eleev / iOS UIFont Names (Raw).swift
Created July 30, 2018 07:19
Use the following reference to properly construct UIFont instances with valid font names
UIFont: family Thonburi
UIFont: font Thonburi-Bold
UIFont: font Thonburi
UIFont: font Thonburi-Light
UIFont: family Khmer Sangam MN
UIFont: font KhmerSangamMN
UIFont: family Snell Roundhand
UIFont: font SnellRoundhand-Black
UIFont: font SnellRoundhand-Bold
UIFont: font SnellRoundhand