Post

Replies

Boosts

Views

Activity

Reply to SwiftUI 4 navigation bug on iOS 16?
Here's a fix, With the help from https://stackoverflow.com/a/60178361/3405069 import SwiftUI import Combine struct AdaptsToKeyboard: ViewModifier { @State var currentHeight: CGFloat = 0 func body(content: Content) -> some View { GeometryReader { geometry in content .padding(.bottom, self.currentHeight) .animation(.easeOut(duration: 0.5), value: currentHeight) .onAppear(perform: { NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillShowNotification) .merge(with: NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillChangeFrameNotification)) .compactMap { notification in withAnimation(.easeOut(duration: 0.16)) { notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect } } .map { rect in rect.height - geometry.safeAreaInsets.bottom } .subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillHideNotification) .compactMap { notification in CGFloat.zero } .subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) }) } } } extension View { func adaptsToKeyboard() -> some View { return modifier(AdaptsToKeyboard()) } } Usage: var body: some View { NavigationView{ ZStack { Color("Background").ignoresSafeArea() VStack{ //Your content here } padding(.bottom) } .adaptsToKeyboard() .edgesIgnoringSafeArea(.bottom) } }
Jul ’23