-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SafeArea Extension, Pin to Superview, and CollectionView Cell Reg…
…istration Extension - Added a SafeArea extension for safe area handling. - Added a Pin to Superview extension for view positioning. - Added an extension for easier registration and dequeuing of cells in collection views.
- Loading branch information
1 parent
40a0eb2
commit c26f532
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
UI/Sources/UI/View/Extension/UICollectionView+RegisterAndDequeue.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// UICollectionView+RegisterAndDequeue.swift | ||
// | ||
// | ||
// Created by Hessam Mahdiabadi on 11/6/23. | ||
// | ||
|
||
import UIKit | ||
|
||
public extension UICollectionView { | ||
|
||
func dequeueReusableCell<T: BaseCollectionCell>(for indexPath: IndexPath) -> T { | ||
let identifier = T.uiIdentifier | ||
guard let cell = dequeueReusableCell(withReuseIdentifier: identifier, | ||
for: indexPath) as? T else { | ||
fatalError("Unable to Dequeue Reusable CollectionView Cell") | ||
} | ||
return cell | ||
} | ||
|
||
func registerReusableCell<T: BaseCollectionCell>(type: T.Type) { | ||
let identifier = T.uiIdentifier | ||
register(T.self, forCellWithReuseIdentifier: identifier) | ||
} | ||
|
||
func registerSupplementaryView<T: UICollectionReusableView & UISequenceIdentifier>(for kind: String, | ||
type: T.Type) { | ||
let identifier = T.uiIdentifier | ||
register(T.self, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier) | ||
} | ||
|
||
func dequeueSupplementaryView<T: UICollectionReusableView & UISequenceIdentifier>(for kind: String, | ||
at indexPath: IndexPath) -> T { | ||
let identifier = T.uiIdentifier | ||
let view = dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) | ||
guard let supplementaryView = view as? T else { | ||
fatalError("Unable to Dequeue Reusable Supplementary View") | ||
} | ||
return supplementaryView | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// UIView+pin.swift | ||
// | ||
// | ||
// Created by Hessam Mahdiabadi on 11/6/23. | ||
// | ||
|
||
import UIKit | ||
|
||
public extension UIView { | ||
|
||
func pinToSuperview() { | ||
guard let superview = self.superview else { | ||
fatalError("unable to find superview, you must first add to any view and then call this method") | ||
} | ||
|
||
NSLayoutConstraint.activate([ | ||
self.topAnchor.constraint(equalTo: superview.topAnchor), | ||
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor), | ||
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor), | ||
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor) | ||
]) | ||
} | ||
|
||
func pinToSuperviewWithSafeArea() { | ||
guard let superview = self.superview else { | ||
fatalError("unable to find superview, you must first add to any view and then call this method") | ||
} | ||
|
||
NSLayoutConstraint.activate([ | ||
self.topAnchor.constraint(equalTo: superview.topSafeMargin), | ||
self.leadingAnchor.constraint(equalTo: superview.leadingSafeMargin), | ||
self.trailingAnchor.constraint(equalTo: superview.trailingSafeMargin), | ||
self.bottomAnchor.constraint(equalTo: superview.bottomSafeMargin) | ||
]) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// UIView+safeArea.swift | ||
// | ||
// | ||
// Created by Hessam Mahdiabadi on 11/6/23. | ||
// | ||
|
||
import UIKit | ||
|
||
public extension UIView { | ||
|
||
var topSafeMargin: NSLayoutYAxisAnchor { | ||
self.safeAreaLayoutGuide.topAnchor | ||
} | ||
|
||
var bottomSafeMargin: NSLayoutYAxisAnchor { | ||
self.safeAreaLayoutGuide.bottomAnchor | ||
} | ||
|
||
var leadingSafeMargin: NSLayoutXAxisAnchor { | ||
self.safeAreaLayoutGuide.leadingAnchor | ||
} | ||
|
||
var trailingSafeMargin: NSLayoutXAxisAnchor { | ||
self.safeAreaLayoutGuide.trailingAnchor | ||
} | ||
|
||
var widthSafeMargin: NSLayoutDimension { | ||
self.safeAreaLayoutGuide.widthAnchor | ||
} | ||
|
||
var heightSafeMargin: NSLayoutDimension { | ||
self.safeAreaLayoutGuide.heightAnchor | ||
} | ||
|
||
var centerXSafeMargin: NSLayoutXAxisAnchor { | ||
self.safeAreaLayoutGuide.centerXAnchor | ||
} | ||
|
||
var centerYSafeMargin: NSLayoutYAxisAnchor { | ||
self.safeAreaLayoutGuide.centerYAnchor | ||
} | ||
} |