hackiftekhar / IQKeyboardManager

Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.
MIT License
16.4k stars 2.41k forks source link

SwiftUI: View with a ScrollView/List is pushed up instead of being resized #1747

Closed MatteCarra closed 3 years ago

MatteCarra commented 3 years ago

Describe the bug 1) Open a swiftui app with a view that has a ScrollView/List and and input field. 2) Click on the input field 3) The view will keep its size and it will be pushed up, with no way to scroll to elements that are not on the current screen.

To Reproduce

import SwiftUI
import IQKeyboardManagerSwift

let TEST = [ "STRING1", "STRING2", "STRING3", "STRING4", "STRING5", "STRING6", "STRING7", "STRING8","STRING9", "STRING10", "STRING11", "STRING12", "STRING13", "STRING14", "STRING15", "STRING16", "STRING17", "STRING18" ]

@main
struct UiTestsApp: App {
    @State var message = ""

    init() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = false
        IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.previousNextDisplayMode = .alwaysHide
    }

    var body: some Scene {
        WindowGroup {
            VStack {
                List {
                    ForEach(TEST, id: \.self) { str in
                        Text(str)
                    }
                }
                TextField("Message...", text: $message)
                    .padding(.all, 7)
                    .cornerRadius(5)

            }
        }
    }
}

Expected behavior The view should be resized. This way the scrollview will take all the space remaining without expanding off-screen and the user can scroll to the elements that don't fit.

Screenshots

Simulator Screen Shot - iPhone 11 Pro Max - 2020-07-29 at 10 03 00

Simulator Screen Shot - iPhone 11 Pro Max - 2020-07-29 at 10 00 26

Versions

Xcode: 12.0 beta 3 Mac OS: 10.10.5 Simulator/Device: iOS 14 Simulator/Device Name: iPhone 11 Pro Max Library Version: 6.5.5

Additional context I have tested the same behaviour with the stable xcode.

MatteCarra commented 3 years ago

I have also tried including the list and input field in a scrollview with scrolling disabled. The keyboard has the expect behaviour, but the list+input field jumps down and up. Video: https://drive.google.com/file/d/185ykiPBEtKBXy7dncRAPnZN2KBDqSrtF/view?usp=sharing The jump usually also happens when the keyboard pops up. Note: scrollView.shouldIgnoreScrollingAdjustment = true had no effect.

import SwiftUI
import IQKeyboardManagerSwift
import Introspect
import Combine

@main
struct UiTestsApp: App {
    @State var message = ""
    @State var TEST = [ "STRING1", "STRING2", "STRING3", "STRING4", "STRING5", "STRING6", "STRING7", "STRING8","STRING9", "STRING10", "STRING11", "STRING12", "STRING13", "STRING14", "STRING15", "STRING16", "STRING17", "STRING18" ]

    init() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = false
        IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.previousNextDisplayMode = .alwaysHide
    }

    func sendMessage() {
        TEST.append(message)
        message = ""
    }

    var body: some Scene {
        WindowGroup {
            ScrollView([], showsIndicators: false) {
                VStack {
                    List {
                        ForEach(TEST, id: \.self) { str in
                            Text(str)
                        }
                    }

                    HStack(spacing: 15) {
                        TextField("Message...", text: $message)
                            .padding(.all, 7)
                            .background(Color.tertiarySystemFill)
                            .cornerRadius(5)

                        Button(action: sendMessage) {
                            Image(systemName: "paperplane.fill")
                                .resizable()
                                .aspectRatio(1, contentMode: .fit)
                                .padding(.all, 3)
                        }
                    }
                    .frame(height: 24)
                    .padding(.horizontal, 8)
                    .padding(.top, 8)

                }
            }
            .introspectScrollView {
                $0.shouldIgnoreScrollingAdjustment = true
            }
        }
    }
}
VatanaChhorn commented 3 years ago

How to do fix the gap between keyboard and textfield?

MatteCarra commented 3 years ago

I think that since Xcode 14.0 beta 3 swiftui automatically avoids the keyboard, and this change is conflicting with IQKeyboard. Removing IQKeyboard solved the issue

VatanaChhorn commented 3 years ago

Lmao, I'm using IQKeyboard just to push the view up whenever clicked on the text field, though. However, I'm working on UIKit, and it takes time to code all those things since the project that I'm working on is not very crucial, and just a learning project. 🤣 Removing IQKeyboard just to solve that case isn't an ideal solution. xD But, thanks for your response anyway, brother.

hakeemdeggs commented 3 years ago

ran into the same issue :/

MatteCarra commented 3 years ago

If you are using x code 12 and swiftui you need to remove iqkeyboard