SwiftKickMobile / SwiftMessages

A very flexible message bar for UIKit and SwiftUI.
MIT License
7.31k stars 743 forks source link

Swift messages not working in Swift UI #327

Open Mteixeira88 opened 5 years ago

Mteixeira88 commented 5 years ago

Hi there,

I'm trying to use Swift Messages on Swift UI and for some reason it's not showing

This is the code I'm trying to use:

.onAppear() {

 let view = MessageView.viewFromNib(layout: .cardView)

view.configureTheme(.warning)

view.configureDropShadow()

let iconText = ["🤔", "😳", "🙄", "😶"].randomElement()!

view.configureContent(title: "Warning", body: "Consider yourself warned.", iconText: iconText)

                view.layoutMarginAdditions = UIEdgeInsets(top: 20, left: 20, bottom: 20, right

                (view.backgroundView as? CornerRoundingView)?.cornerRadius = 10

                SwiftMessages.show(view: view)

        }

I already tried to use in a button click and it didn't work. Can you help? Thanks

wtmoose commented 5 years ago

I likely won't have time to look into SwiftUI anytime soon.

Mteixeira88 commented 5 years ago

I likely won't have time to look into SwiftUI anytime soon.

Thanks for answering

Jasperav commented 5 years ago

That would be great because than we can use it in SPM

wtmoose commented 5 years ago

@Jasperav huh?

Jasperav commented 5 years ago

Swift Package Manager supports SwiftUI, not UIKit. We can get rid of Cocoapods/Cartage and use a native dependency manager instead.

wtmoose commented 5 years ago

@Jasperav I view this as a compatibility ticket. It's not about converting the library to SwiftUI.

sheikhumar93 commented 3 years ago

For anyone looking to use this library in their SwiftUI projects, technically all UIKit code works with SwiftUI. You have to use the UIViewControllerRepresentable protocol to achieve this. I just implemented the Bottom Message Segue in this library using pretty much the same setup in Storyboard as shown in the Demo. Then implement code like this to use it from SwiftUI

struct BottomMessageSegue: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> some UIViewController {
        let storyboard = UIStoryboard(name: "Storyboard", bundle: Bundle.main)
        let vc = storyboard.instantiateViewController(identifier: "InitialVC")
        return vc
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {

    }

}

Replace identifier with your own. Create a View Controller and assign it to the root View Controller Scene in the Storyboard Again pretty much the same code as shown in the Demo

import UIKit
import SwiftMessages

class ViewControllersViewController: UIViewController {
    @objc @IBAction private func dismissPresented(segue: UIStoryboardSegue) {
        dismiss(animated: true, completion: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        performSegue(withIdentifier: "showTools", sender: nil)
    }
}

class SwiftMessagesBottomSegue: SwiftMessagesSegue {
    override public init(identifier: String?, source: UIViewController, destination: UIViewController) {
        super.init(identifier: identifier, source: source, destination: destination)
        configure(layout: .bottomMessage)
    }
}

with the addition of performSegue() in the viewDidAppear(), replace the segue identifier with your own and you are done.

Use BottomMessageSegue() as you would use any other SwiftUI view and it will just work without any problems. Have fun!

hernangonzalez commented 2 years ago

So this issue can actually be closed with a README update, right?