How to show complete List when keyboard is showing up in SwiftUI
there is an answer here to handle keyboard actions,
you can subscribe for keyboard events like this:
final class KeyboardResponder: BindableObject {
let didChange = PassthroughSubject<CGFloat, Never>()
private var _center: NotificationCenter
private(set) var currentHeight: CGFloat = 0 {
didSet {
didChange.send(currentHeight)
}
}
init(center: NotificationCenter = .default) {
_center = center
_center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
_center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
_center.removeObserver(self)
}
@objc func keyBoardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
currentHeight = keyboardSize.height
}
}
@objc func keyBoardWillHide(notification: Notification) {
currentHeight = 0
}
}
and then just use it like this:
@State var keyboard = KeyboardResponder()
var body: some View {
List {
VStack {
...
...
...
}.padding(.bottom, keyboard.currentHeight)
}
SwiftUI List with TextField adjust when keyboard appears/disappears
The resolution for the problem with the keyboard padding is like E.coms suggested. Also the class written here by kontiki can be used:
How to make the bottom button follow the keyboard display in SwiftUI
The problems I had was because of state changes in my view hierarchy due to multiple instances of reference types publishing similar state changes.
My view models are reference types, which publish changes to its models, which are value types. However, these view models also contain reference types which handle network requests. For each view I render (each row), I assign a new view model instance, which also creates a new network service instance. Continuing this pattern, each of these network services also create and assign new network managers.
How to detect when keyboard is shown and hidden
In the ViewDidLoad method of your class set up to listen for messages about the keyboard:
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Then in the methods you specify (in this case keyboardDidShow
and keyboardDidHide
) you can do something about it:
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
}
iOS 14 SwiftUI Keyboard lifts view automatically
You should apply the modifier on the ZStack
, NOT the NavigationView
NavigationView(content: {
ZStack{
,,,
}.navigationBarHidden(true)
.navigationBarTitle("")
.ignoresSafeArea(.keyboard, edges: .bottom) // <- This line moved up
})
Full working example:
struct ContentView: View {
@State var text = ""
var body: some View {
VStack{
Spacer()
Text("Hello, World")
TextField("Tap to test keyboard ignoring", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding()
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
Related Topics
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
Uitableview Disable Swipe to Delete, But Still Have Delete in Edit Mode
When to Use Uicollectionview Instead of Uitableview
Set Uitableview Content Inset Permanently
iOS App Submission and Beta Review Process
Launch a Local Notification at a Specific Time in iOS
How to Ask User for Camera Access After They Have Already Denied It on iOS
How to Pop Two Views at Once from a Navigation Controller
Adding Multiple Arrays to Form One Final Array. Debug Swift Xcode
Uicollectionview with a Sticky Header
How to Make Uibutton's Text Alignment Center? Using Ib
Drawrect Circle and Animate Size/Color
iOS App, Programmatically Get Build Version
iOS - Cydia Open Command and Its Counterpart
Xcode/Simulator: How to Run Older iOS Version
Randomly Choosing an Item from a Swift Array Without Repeating
Differences Between Websockets and Long Polling for Turn Based Game Server