IQKeyboardManager icon indicating copy to clipboard operation
IQKeyboardManager copied to clipboard

SwiftUI Support

Open MahranBayed opened this issue 4 years ago • 17 comments

I wonder how we can use the IQKeyboardManager in SwiftUI.

Is there is a way to use it with toolbarPreviousNextAllowedClasses ?

MahranBayed avatar Sep 25 '19 11:09 MahranBayed

Sorry, I current do not know how we can use library functions in SwiftUI. However all default functions works with SwiftUI projects. Please also let me know if you know something about it.

hackiftekhar avatar Sep 26 '19 05:09 hackiftekhar

So far I got some things working, but the IQPreviousNext isn't one of them

PedroCavaleiro avatar Dec 05 '19 18:12 PedroCavaleiro

@PedroCavaleiro Can you please share your experience with swiftUI and IQKeyboardManager?

hackiftekhar avatar Dec 05 '19 18:12 hackiftekhar

@hackiftekhar So far good, haven’t found any bugs on the features that I use (dismiss when tapped outside the TextField, changed the name of the toolbar button Done button and forced the next/previous keys to show up) Only the previous/next isn’t working for me, neither on the toolbar or the keyboard (return/next/done) key The next/previous buttons, if forced, they appear if installed using podfile

PedroCavaleiro avatar Dec 05 '19 19:12 PedroCavaleiro

@PedroCavaleiro can you share the way that you've done it please ?

MaxenceMax avatar Jan 15 '20 16:01 MaxenceMax

Sorry for the delayed response @MaxenceMax but I was extremely sick and did not used my computer until now.

On the AppDelegate.swift

IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "Terminar"
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
IQKeyboardManager.shared.previousNextDisplayMode = .alwaysShow

PedroCavaleiro avatar Jan 20 '20 03:01 PedroCavaleiro

I did a little bit testing with SwiftUI, the next/previous arrows do not appear because the SwiftUI creates a different hierarchy of views than usual. I was trying to implement some kind of IQPreviousNextViewSwiftUI which we can use as a container to enable previous/next buttons, but not able to fully support it.

hackiftekhar avatar Jan 20 '20 14:01 hackiftekhar

are there going to be work to make next/previous arrow work for swiftUI? would love that.

TakaGoto avatar Jun 01 '20 17:06 TakaGoto

It would be easier to make some workaround by peoples who already know how to do it with SwiftUI. Unfortunately I'm not one of them till now, haven't got chance to learn SwiftUI.

hackiftekhar avatar Jun 19 '20 21:06 hackiftekhar

Hi all, I discovered that pressing the "Done" button in SwiftUi doesn't trigger the "on commit:" in the SwiftUi TextField. Any Idea how to change this behaviour? Especially, I am using the keyboardTye(.decimalPad) option where the IQKeyboardManager-Done button would come in super handy committing the keyboard and finishing the entry in the SwiftUI TextField as well. Pressing return on a the default keyboard works just fine. Thanks

Heye42 avatar Aug 15 '20 22:08 Heye42

In Xcode 12 there is no any AppDelegate and we now views will allow only struct, class,enum. If anyone has implemented IQKeyboard for swiftui then please share few tips.

dipak-onehop avatar Sep 30 '20 13:09 dipak-onehop

You can still implement it, go to the file where the @main is located, in the example below it's called your_projectApp.swift

// MARK: Add from here
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        IQKeyboardManager.shared.enable = true
        return true
    }
}
// MARK: To Here

@main
struct your_projectApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // add this line in the struct
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

PedroCavaleiro avatar Oct 01 '20 13:10 PedroCavaleiro

You can still implement it, go to the file where the @main is located, in the example below it's called your_projectApp.swift

// MARK: Add from here
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        IQKeyboardManager.shared.enable = true
        return true
    }
}
// MARK: To Here

@main
struct your_projectApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // add this line in the struct
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

It will display only Done button and Next, Previous is not working

dipak-onehop avatar Oct 11 '20 06:10 dipak-onehop

You can still implement it, go to the file where the @main is located, in the example below it's called your_projectApp.swift

// MARK: Add from here
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        IQKeyboardManager.shared.enable = true
        return true
    }
}
// MARK: To Here

@main
struct your_projectApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // add this line in the struct
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

It will display only Done button and Next, Previous is not working

That's a problem that I've yet to solve.

PedroCavaleiro avatar Oct 13 '20 10:10 PedroCavaleiro

any update on " Next, Previous is not working"? I checked today and it's the same result.

skywalkerlw avatar Oct 26 '21 20:10 skywalkerlw

I haven't learned the SwiftUI yet. So I need help from peoples who already learn the swift UI to write something same like IQPreviousNextView class implemented for the Swift/ObjC.

hackiftekhar avatar Nov 08 '21 06:11 hackiftekhar

For me it will crash on search when i click on textfield.

ParagDevac avatar Jan 27 '22 12:01 ParagDevac

I learned the SwiftUI now and I found that it's not easy to use this library directly in SwiftUI because SwiftUI has it's own mechanism to handle keyboard input and show the textField at correct position by moving it's UI. However we may have something for previous/next and done. But in those cases onCommit doesn't fire probably.

Due to these things, I think it's not a good idea at the moment to support it for SwiftUI as the general purpose use of the library is already handled by SwiftUI.

hackiftekhar avatar May 18 '23 15:05 hackiftekhar

@PedroCavaleiro Can you please share your experience with swiftUI and IQKeyboardManager?

it is not showing tool bar in swiftui

abhishekquantic avatar Jun 14 '23 21:06 abhishekquantic

Since I started targeting iOS 15 I no longer use IQKeyboardManager

I felt that the OS was doing its thing and you could add the toolbar items on SwiftUI. I no longer know how this lib is behaving.

But as @hackiftekhar said

I learned the SwiftUI now and I found that it's not easy to use this library directly in SwiftUI because SwiftUI has it's own mechanism to handle keyboard input and show the textField at correct position by moving it's UI. However we may have something for previous/next and done. But in those cases onCommit doesn't fire probably.

Due to these things, I think it's not a good idea at the moment to support it for SwiftUI as the general purpose use of the library is already handled by SwiftUI.

So this lib is really not suitable to use with SwiftUI specially in the latest versions of iOS

PedroCavaleiro avatar Jun 14 '23 21:06 PedroCavaleiro