SwiftMessages icon indicating copy to clipboard operation
SwiftMessages copied to clipboard

Swift messages not working in Swift UI

Open Mteixeira88 opened this issue 6 years ago • 8 comments

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

Mteixeira88 avatar Jul 18 '19 10:07 Mteixeira88

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

wtmoose avatar Jul 18 '19 15:07 wtmoose

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

Thanks for answering

Mteixeira88 avatar Jul 18 '19 16:07 Mteixeira88

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

Jasperav avatar Sep 06 '19 13:09 Jasperav

@Jasperav huh?

wtmoose avatar Sep 06 '19 14:09 wtmoose

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

Jasperav avatar Sep 06 '19 18:09 Jasperav

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

wtmoose avatar Sep 07 '19 16:09 wtmoose

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!

sheikhumar93 avatar May 18 '21 06:05 sheikhumar93

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

hernangonzalez avatar Dec 07 '21 11:12 hernangonzalez