Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift messages not working in Swift UI #327

Open
Mteixeira88 opened this issue Jul 18, 2019 · 8 comments
Open

Swift messages not working in Swift UI #327

Mteixeira88 opened this issue Jul 18, 2019 · 8 comments

Comments

@Mteixeira88
Copy link

Mteixeira88 commented Jul 18, 2019

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
Copy link
Member

wtmoose commented Jul 18, 2019

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

@Mteixeira88
Copy link
Author

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

Thanks for answering

@Jasperav
Copy link

Jasperav commented Sep 6, 2019

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

@wtmoose
Copy link
Member

wtmoose commented Sep 6, 2019

@Jasperav huh?

@Jasperav
Copy link

Jasperav commented Sep 6, 2019

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

@wtmoose
Copy link
Member

wtmoose commented Sep 7, 2019

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

@sheikhumar93
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants