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
struct KeyboardAvoiderPreference: Equatable { | |
let tag: Int | |
let rect: CGRect | |
static func == (lhs: KeyboardAvoiderPreference, rhs: KeyboardAvoiderPreference) -> Bool { | |
print("y: \(lhs.rect.minY) vs \(rhs.rect.minY)") | |
return lhs.tag == rhs.tag && (lhs.rect.minY == rhs.rect.minY) | |
} | |
} |
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
struct KeyboardAvoiderPreferenceReader: ViewModifier { | |
let tag: Int | |
func body(content: Content) -> some View { | |
content | |
.background( | |
GeometryReader { geometry in | |
Rectangle() |
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
extension View { | |
func avoidKeyboard(tag: Int) -> some View { | |
self.modifier(KeyboardAvoiderPreferenceReader(tag: tag)) | |
} | |
} |
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
extension View { | |
func registerKeyboardAvoider(_ avoider: KeyboardAvoider) -> some View { | |
self.onPreferenceChange(KeyboardAvoiderPreferenceKey.self) { prefs in | |
prefs.forEach { pref in | |
print("Avoider Rect: \(pref.rect)") | |
avoider.rects[pref.tag] = pref.rect | |
} |
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
struct KeyboardAvoiding<Content>: View where Content: View{ | |
@ObservedObject private var avoider : KeyboardAvoider | |
let content: () -> Content | |
let offset: CGFloat | |
init(with avoider: KeyboardAvoider, offset: CGFloat = 0, @ViewBuilder content: @escaping () -> Content) { | |
self.content = content | |
self.avoider = avoider |
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
struct ScrollableView: View { | |
@State var value1: String = "" | |
@State var value2: String = "" | |
@ObservedObject var avoider = KeyboardAvoider() | |
var body: some View { | |
NavigationView { |
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
struct StaticView: View { | |
@State var value1: String = "" | |
@State var value2: String = "" | |
@ObservedObject var avoider = KeyboardAvoider() | |
var body: some View { | |
KeyboardAvoiding(with: avoider) { |
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
extension CollectionView { | |
class Coordinator: NSObject, UICollectionViewDelegate { | |
// MARK: - Properties | |
let parent: CollectionView | |
var dataSource: UICollectionViewDiffableDataSource<Section, Item>? | |
// MARK: - Init | |
init(_ parent: CollectionView) { |
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
extension CollectionView { | |
private func snapshotForCurrentState() -> NSDiffableDataSourceSnapshot<Section, Item> { | |
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() | |
snapshot.appendSections(sections) | |
for section in sections { | |
snapshot.appendItems(items[section]!, toSection: section) | |
} | |
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
extension CollectionView { | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func makeUIViewController(context: Context) -> CollectionViewController<Section, Item> { | |
print("CollectionView.make()") | |
let controller = CollectionViewController<Section, Item>() | |
controller.layout = layout |