Skip to content

Commit

Permalink
Extension updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince2k3 committed Feb 1, 2020
1 parent 9b4b850 commit 9179363
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 111 deletions.
6 changes: 2 additions & 4 deletions Sources/UIKitAdditions/Extensions/UIButton+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import UIKit

extension UIButton {
public func setBackgroundColor(_ color: UIColor?, for state: UIControl.State) {
guard let color = color else {
return setBackgroundImage(nil, for: state)
}
setBackgroundImage(.makeImage(with: color), for: state)
return color.flatMap { setBackgroundImage($0.fillImage(), for: state) } ??
setBackgroundImage(nil, for: state)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ extension UICollectionView {
public func register<T: Identifiable>(`class` cls: T.Type, forSupplementaryViewOfKind kind: String) {
register(cls.self as? AnyClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: cls.identifier)
}

public func dequeueReusableCell<T: Identifiable>(_ type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withReuseIdentifier: type.identifier, for: indexPath) as! T
}

public func dequeueReusableSupplementaryView<T: Identifiable>(ofKind elementKind: String, type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: type.identifier, for: indexPath) as! T
}
}

extension UICollectionView {
Expand Down
42 changes: 42 additions & 0 deletions Sources/UIKitAdditions/Extensions/UIColor+Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import UIKit

extension UIColor {
public func fillImage(_ size: CGSize = .init(width: 1, height: 1), cornerRadius: CGFloat = 0.0) -> UIImage {
return UIGraphicsImageRenderer(size: size)
.image { _ in
self.setFill()
let bezierPath = UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: cornerRadius)
bezierPath.fill()
}
}

public func fillCircleImage(_ size: CGSize = .init(width: 1, height: 1)) -> UIImage {
let cornerRadius = min(size.width, size.height) / 2.0
return fillImage(size, cornerRadius: cornerRadius)
}

public func fillImage(_ size: CGSize = .init(width: 1, height: 1)) -> UIImage {
return UIGraphicsImageRenderer(size: size)
.image { _ in
self.setFill()
let bezierPath = UIBezierPath(rect: CGRect(origin: .zero, size: size))
bezierPath.fill()
}
}

public func strokeCircleImage(_ size: CGSize, lineWidth: CGFloat = 2) -> UIImage {
let cornerRadius = min(size.width, size.height) / 2.0
return strokeImage(size, cornerRadius: cornerRadius, lineWidth: lineWidth)
}

public func strokeImage(_ size: CGSize, cornerRadius: CGFloat = 0.0, lineWidth: CGFloat = 2) -> UIImage {
return UIGraphicsImageRenderer(size: size)
.image { _ in
self.setStroke()
let bezierPath = UIBezierPath(roundedRect: CGRect(origin: .zero, size: size).insetBy(dx: lineWidth, dy: lineWidth), cornerRadius: cornerRadius)
bezierPath.lineWidth = lineWidth
bezierPath.stroke()

}
}
}
21 changes: 21 additions & 0 deletions Sources/UIKitAdditions/Extensions/UIControl+Handler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import UIKit

extension UIControl {
func add (events: UIControl.Event, for handler: @escaping () -> Void) {
let handler = HandlerWrapper(handler)
addTarget(handler, action: #selector(HandlerWrapper.invoke), for: events)
objc_setAssociatedObject(self, String(ObjectIdentifier(self).hashValue) + String(events.rawValue), handler, .OBJC_ASSOCIATION_RETAIN)
}
}

class HandlerWrapper {
let handler: () -> Void

init (_ handler: @escaping () -> Void) {
self.handler = handler
}

@objc func invoke () {
handler()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

extension UIEdgeInsets {
public init(bottom: CGFloat = 0.0, right: CGFloat = 0.0, top: CGFloat = 0.0, left: CGFloat = 0.0) {
self.init(top: top, left: left, bottom: bottom, right: right)
public init(size: CGFloat) {
self.init(top: size, left: size, bottom: size, right: size)
}
}
15 changes: 0 additions & 15 deletions Sources/UIKitAdditions/Extensions/UIImage+Extension.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import UIKit

extension UINavigationController {
@discardableResult
func popToViewController(type: UIViewController.Type, animated: Bool) -> [UIViewController]? {
guard
let foundViewController = self.viewControllers.filter({ $0.identifier == type.identifier }).first
else { return nil }
return popToViewController(foundViewController, animated: true)
}
}

extension UINavigationController {
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return self.viewControllers.first?.preferredInterfaceOrientationForPresentation ?? super.preferredInterfaceOrientationForPresentation
}

open override var transitioningDelegate: UIViewControllerTransitioningDelegate? {
get {
return self.viewControllers.first?.transitioningDelegate ?? super.transitioningDelegate
}

set {

if let rootViewController = self.viewControllers.first {
rootViewController.transitioningDelegate = newValue
} else {
super.transitioningDelegate = newValue
}
}
}

open override var preferredStatusBarStyle: UIStatusBarStyle {
return self.viewControllers.first?.preferredStatusBarStyle ?? super.preferredStatusBarStyle
}

open override var prefersStatusBarHidden: Bool {
return self.viewControllers.first?.prefersStatusBarHidden ?? super.prefersStatusBarHidden
}

open override var modalPresentationStyle: UIModalPresentationStyle {
get {
return self.viewControllers.first?.modalPresentationStyle ?? super.modalPresentationStyle
}

set {
if let rootViewController = self.viewControllers.first {
rootViewController.modalPresentationStyle = newValue
} else {
super.modalPresentationStyle = newValue
}
}
}

open override var modalTransitionStyle: UIModalTransitionStyle {
get {
return self.viewControllers.first?.modalTransitionStyle ?? super.modalTransitionStyle
}

set {
if let rootViewController = self.viewControllers.first {
rootViewController.modalTransitionStyle = newValue
} else {
super.modalTransitionStyle = newValue
}
}
}

open override var shouldAutorotate: Bool {
return self.topViewController?.shouldAutorotate ?? super.shouldAutorotate
}
}
//extension UINavigationController {
// @discardableResult
// func popToViewController(type: UIViewController.Type, animated: Bool) -> [UIViewController]? {
// guard
// let foundViewController = self.viewControllers.filter({ $0.identifier == type.identifier }).first
// else { return nil }
// return popToViewController(foundViewController, animated: true)
// }
//}
//
//extension UINavigationController {
// open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
// return self.viewControllers.first?.preferredInterfaceOrientationForPresentation ?? super.preferredInterfaceOrientationForPresentation
// }
//
// open override var transitioningDelegate: UIViewControllerTransitioningDelegate? {
// get {
// return self.viewControllers.first?.transitioningDelegate ?? super.transitioningDelegate
// }
//
// set {
//
// if let rootViewController = self.viewControllers.first {
// rootViewController.transitioningDelegate = newValue
// } else {
// super.transitioningDelegate = newValue
// }
// }
// }
//
// open override var preferredStatusBarStyle: UIStatusBarStyle {
// return self.viewControllers.first?.preferredStatusBarStyle ?? super.preferredStatusBarStyle
// }
//
// open override var prefersStatusBarHidden: Bool {
// return self.viewControllers.first?.prefersStatusBarHidden ?? super.prefersStatusBarHidden
// }
//
// open override var modalPresentationStyle: UIModalPresentationStyle {
// get {
// return self.viewControllers.first?.modalPresentationStyle ?? super.modalPresentationStyle
// }
//
// set {
// if let rootViewController = self.viewControllers.first {
// rootViewController.modalPresentationStyle = newValue
// } else {
// super.modalPresentationStyle = newValue
// }
// }
// }
//
// open override var modalTransitionStyle: UIModalTransitionStyle {
// get {
// return self.viewControllers.first?.modalTransitionStyle ?? super.modalTransitionStyle
// }
//
// set {
// if let rootViewController = self.viewControllers.first {
// rootViewController.modalTransitionStyle = newValue
// } else {
// super.modalTransitionStyle = newValue
// }
// }
// }
//
// open override var shouldAutorotate: Bool {
// return self.topViewController?.shouldAutorotate ?? super.shouldAutorotate
// }
//}
18 changes: 18 additions & 0 deletions Sources/UIKitAdditions/Extensions/UIStackView+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import UIKit

extension UIStackView {
public func addArrangedSubviews(_ views: [UIView]) {
views.forEach { addArrangedSubview($0) }
}

@discardableResult
public func removeAllArrangedSubviews() -> [UIView] {
let removedSubviews = arrangedSubviews.reduce([]) { (removedSubviews, subview) -> [UIView] in
self.removeArrangedSubview(subview)
NSLayoutConstraint.deactivate(subview.constraints)
subview.removeFromSuperview()
return removedSubviews + [subview]
}
return removedSubviews
}
}
8 changes: 8 additions & 0 deletions Sources/UIKitAdditions/Extensions/UITableView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ extension UITableView {
public func registerHeaderFooter<T: Identifiable>(`class` cls: T.Type) {
register(cls.self as? AnyClass, forHeaderFooterViewReuseIdentifier: cls.identifier)
}

public func dequeueReusableCell<T: Identifiable>(_ type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withIdentifier: type.identifier, for: indexPath) as! T
}

public func dequeueReusableHeaderFooterView<T: Identifiable>(_ type: T.Type) -> T? {
return dequeueReusableHeaderFooterView(withIdentifier: type.identifier) as? T
}
}
Loading

0 comments on commit 9179363

Please sign in to comment.