session-ios/SignalMessaging/categories/UIView+OWS.swift

90 lines
3.0 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
public extension UIEdgeInsets {
public init(top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) {
self.init(top: top,
left: CurrentAppContext().isRTL ? trailing : leading,
bottom: bottom,
right: CurrentAppContext().isRTL ? leading : trailing)
}
}
@objc
public extension UINavigationController {
@objc
public func pushViewController(viewController: UIViewController,
animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
pushViewController(viewController, animated: animated)
CATransaction.commit()
}
@objc
public func popViewController(animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popViewController(animated: animated)
CATransaction.commit()
}
@objc
public func popToViewController(viewController: UIViewController,
animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.popToViewController(viewController, animated: animated)
CATransaction.commit()
}
}
2019-02-12 16:03:32 +01:00
extension UIView {
public func renderAsImage() -> UIImage? {
return renderAsImage(opaque: false, scale: UIScreen.main.scale)
}
public func renderAsImage(opaque: Bool, scale: CGFloat) -> UIImage? {
if #available(iOS 10, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
format.opaque = opaque
let renderer = UIGraphicsImageRenderer(bounds: self.bounds,
format: format)
return renderer.image { (context) in
self.layer.render(in: context.cgContext)
}
} else {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)
if let _ = UIGraphicsGetCurrentContext() {
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
owsFailDebug("Could not create graphics context.")
return nil
}
}
2019-02-12 20:56:58 +01:00
@objc
public class func spacer(withWidth width: CGFloat) -> UIView {
let view = UIView()
view.autoSetDimension(.width, toSize: width)
return view
}
@objc
public class func spacer(withHeight height: CGFloat) -> UIView {
let view = UIView()
view.autoSetDimension(.height, toSize: height)
return view
}
2019-02-12 16:03:32 +01:00
}