Created
August 20, 2024 02:38
-
-
Save gavinmn/776f43a379152f625a6f8200ddf9042c to your computer and use it in GitHub Desktop.
Attach a view to the keyboard with interactive dismissal support
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
// | |
// ContentView.Swift | |
// KeyboardAttachedView | |
// | |
// Created by Gavin Nelson on 8/19/24. | |
// | |
import SwiftUI | |
import UIKit | |
struct ContentView: View { | |
var body: some View { | |
ZStack(alignment: .bottom) { | |
VStack { | |
ScrollView { | |
ForEach(0..<20) { index in | |
HStack { | |
Text("Item \(index + 1)") | |
.padding() | |
Spacer() | |
} | |
} | |
} | |
.scrollDismissesKeyboard(.interactively) | |
.scrollClipDisabled() | |
KeyboardAttachedView() | |
.frame(height: 44) | |
} | |
} | |
.ignoresSafeArea(.keyboard) | |
} | |
} | |
struct TextInputView: View { | |
@State private var textFieldText = "" | |
var body: some View { | |
TextField("Type here", text: $textFieldText) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding() | |
} | |
} | |
struct KeyboardAttachedView: UIViewControllerRepresentable { | |
func makeUIViewController(context: Context) -> KeyboardObservingViewController { | |
return KeyboardObservingViewController() | |
} | |
func updateUIViewController(_ uiViewController: KeyboardObservingViewController, context: Context) {} | |
} | |
class KeyboardObservingViewController: UIViewController { | |
private var bottomConstraint: NSLayoutConstraint? | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
let textView = TextInputView() | |
let hostView = UIHostingController(rootView: textView) | |
hostView.view.translatesAutoresizingMaskIntoConstraints = false | |
hostView.view.backgroundColor = .clear | |
view.addSubview(hostView.view) | |
NSLayoutConstraint.activate([ | |
hostView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
hostView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
hostView.view.heightAnchor.constraint(equalToConstant: 44) | |
]) | |
bottomConstraint = hostView.view.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor) | |
bottomConstraint?.isActive = true | |
view.keyboardLayoutGuide.topAnchor.constraint(equalTo: hostView.view.bottomAnchor).isActive = true | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidDisappear(animated) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment