Skip to content

Instantly share code, notes, and snippets.

View Anatoli-Petrosyants's full-sized avatar

AnatoliPetrosyants Anatoli-Petrosyants

View GitHub Profile
import RxSwift
import RxCocoa
func keyboardHeight() -> Observable<CGFloat> {
return Observable
.from([
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillShow)
.map { notification -> CGFloat in
(notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0
},
import Foundation
import UIKit
struct BaseStyles {
static func rounded<V: UIView>(cornerRadius r: CGFloat = 5.5) -> UIViewStyle<V> {
return UIViewStyle<V>(styling: { (view: V) in
view.layer.masksToBounds = true
view.clipsToBounds = true
view.layer.cornerRadius = r
})
import Foundation
import UIKit
// MARK: Protocol Definition
/// Make your UIView subclasses conform to this protocol when:
/// * they *are* NIB-based, and
/// * this class is used as the XIB's root view
///
/// to be able to instantiate them from the NIB in a type-safe manner
public protocol NibLoadable: class {
import Foundation
import UIKit
class Notification {
// Shows a Notification from a NotificationType
public static func showNotification(type: NotificationType, completion: @escaping () -> () = {}) {
let view = NotificationView()
view.setBackgroundColor(color: type.backgroundColor)
extension UIColor {
convenience init(hexString: String, alpha: CGFloat = 1.0) {
let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let scanner = Scanner(string: hexString)
if (hexString.hasPrefix("#")) {
scanner.scanLocation = 1
}
var color: UInt32 = 0
scanner.scanHexInt32(&color)
extension UIApplication {
static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
}
extension UIApplication {
class func topViewController(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = viewController as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = viewController as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
extension UIButton {
// https://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted
private func image(withColor color: UIColor) -> UIImage? {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
extension CGFloat {
static var random: CGFloat {
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
}
extension UIColor {
static var random: UIColor {
return UIColor(red: .random, green: .random, blue: .random, alpha: 1.0)
}
extension Int {
func toString() -> String {
return "\(self)"
}
func toObjCString() -> NSString {
return NSString(format: "%d", self)
}
func toDouble() -> Double {