Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load View with Bundle #77

Merged
merged 1 commit into from
Apr 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Utils/Utils/UIView/UIView+XibSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import UIKit

public extension UIView {
func xibSetup() {
let view = loadFromNib()
func xibSetup(bundle: Bundle) {
let view = loadFromNib(bundle: bundle)
addSubview(view)
stretch(view: view)
}

func loadFromNib<T: UIView>() -> T {
func loadFromNib<T: UIView>(bundle: Bundle) -> T {
let selfType = type(of: self)
let bundle = Bundle(for: selfType)
let nibName = String(describing: selfType)
let nib = UINib(nibName: nibName, bundle: bundle)

Expand All @@ -28,8 +27,7 @@ public extension UIView {
return view
}

static func loadFromNib<T: UIView>() -> T {
let bundle = Bundle(for: self)
static func loadFromNib<T: UIView>(bundle: Bundle) -> T {
let nibName = String(describing: self)
let nib = UINib(nibName: nibName, bundle: bundle)

Expand All @@ -40,6 +38,22 @@ public extension UIView {
return view
}

func xibSetup() {
let view = loadFromNib()
addSubview(view)
stretch(view: view)
}

func loadFromNib<T: UIView>() -> T {
let bundle = Bundle(for: type(of: self))
return loadFromNib(bundle: bundle)
}

static func loadFromNib<T: UIView>() -> T {
let bundle = Bundle(for: self)
return loadFromNib(bundle: bundle)
}

func stretch(view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
Expand Down