Skip to content

Commit

Permalink
[#3] extension 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ezidayzi committed Sep 15, 2021
1 parent d3311da commit b67561f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIButton+setBackgroundColor.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/15.
//

import UIKit

extension UIButton {
func setBackgroundColor(_ color: UIColor, for state: UIControl.State) {
UIGraphicsBeginImageContext(CGSize(width: 1.0, height: 1.0))
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setFillColor(color.cgColor)
context.fill(CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0))

let backgroundImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

setBackgroundImage(backgroundImage, for: state)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// UIStackView+addArrangedSubviews.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/14.
//

import UIKit

extension UIStackView {
func addArrangedSubviews(_ views: UIView...) {
for view in views { addArrangedSubview(view) }
}
}
44 changes: 44 additions & 0 deletions CheoMooRac/CheoMooRac/Sources/Exensions/UITableView+Generic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// UITableView+Generic.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/14.
//

import UIKit

protocol Reusable: AnyObject {
static var reuseIdentifier: String { get }
static var nib: UINib? { get }
}

extension Reusable {
static var reuseIdentifier: String { return String(describing: self) }
static var nib: UINib? { return nil }
}

extension UITableView {
func registerReusableCell<T: UITableViewCell>(_: T.Type) where T: Reusable {
if let nib = T.nib {
self.register(nib, forCellReuseIdentifier: T.reuseIdentifier)
} else {
self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
}
}

func dequeueReusableCell<T: UITableViewCell>(indexPath: IndexPath) -> T where T: Reusable {
return self.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as! T
}

func registerReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_: T.Type) where T: Reusable {
if let nib = T.nib {
self.register(nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
} else {
self.register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
}
}

func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>() -> T where T: Reusable {
return self.dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as! T
}
}
14 changes: 14 additions & 0 deletions CheoMooRac/CheoMooRac/Sources/Exensions/UIView+addSubviews.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// UIView+addSubviews.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/14.
//

import UIKit

extension UIView {
func addSubviews(_ views: UIView...) {
for view in views { addSubview(view) }
}
}

0 comments on commit b67561f

Please sign in to comment.