How to Show Complete List When Keyboard Is Showing Up in Swiftui

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)
}
}

Demo



Related Topics



Leave a reply



Submit