How to Toast message in Swift?

后端 未结 22 1011
暖寄归人
暖寄归人 2020-12-22 19:18

Is there any way to Toast message in swift ?

I have tried in objective c but could not find solution in swift.

[self.view makeToast:@\"Account create         


        
相关标签:
22条回答
  • 2020-12-22 19:39

    if makeToast:duration:position: is defined in objective-c and can be called, then the swift code will be

    self.view.makeToast("Acount created Successfully", duration: 0.5, position: "bottom")
    

    You may need to use a bridging header to gain access to those method in your swift code though.

    0 讨论(0)
  • 2020-12-22 19:39

    This will help for you it will make the toast at the center with proper padding

    func showToast(message:String,view:UIView){
        let toastLabel = PaddingLabel()
        toastLabel.frame = CGRect(x:0, y: view.frame.size.height-100, width: view.frame.width-50, height: 0)
        toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        toastLabel.textColor = UIColor.white
        toastLabel.textAlignment = .center;
        toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
        toastLabel.text = message
        toastLabel.alpha = 1.0
        toastLabel.layer.cornerRadius = 10;
        toastLabel.clipsToBounds  =  true
        toastLabel.sizeToFit()
        toastLabel.frame.origin.x=(view.frame.width/2)-(toastLabel.frame.width/2)
        view.addSubview(toastLabel)
        UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
            toastLabel.alpha = 0.0
        }, completion: {(isCompleted) in
            toastLabel.removeFromSuperview()
        })
    }
    

    And add this PaddingLabel file for label padding

    import Foundation
    import UIKit
    class PaddingLabel: UILabel {
    
    let padding=UIEdgeInsetsMake(5, 10, 5,10)
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
    }
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        let superSizeThatFits=super.sizeThatFits(size)
        let width=superSizeThatFits.width+padding.left+padding.right
        let height=superSizeThatFits.height+padding.top+padding.bottom
        return CGSize(width: width, height: height)
    }
    }
    
    0 讨论(0)
  • 2020-12-22 19:40

    Let me add this to this chain of answers: This library does what you need DCToastView allowing you to provide toast messages from the top or bottom side of the screen:

    You will just need to add the pod

    pod 'DCToastView'

    Import it where you want to use it.

    import DCToastView

    And use it

    ToastPresenter.shared.show(in: self.view, message: "This is a toast")

    You can pass the following properties to the show method:

    • view: The view in which the toast is going to be presented
    • message: The message that the toast will show
    • toastPlace: The place which can be .down or .up
    • backgroundColor: The background color of the toast; defaults to black
    • textColor: The text color of the message; defaults to white
    • timeOut: The amount of seconds for the toast to dismiss if not provided it means that the toast will be sticky (will remain until touched); defaults to nil
    • roundness: How round the toast will be: .none, .low, .mid, .high; defaults to .mid
    0 讨论(0)
  • 2020-12-22 19:44

    Instead of using UILabel using UITextView gets better results.

    func showToast(message: String) {
            let toastLabel = UITextView(frame: CGRect(x: self.view.frame.size.width/16, y: self.view.frame.size.height-150, width: self.view.frame.size.width * 7/8, height: 35))
            toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
            toastLabel.textColor = UIColor.white
            toastLabel.textAlignment = .center;
            toastLabel.text = "   \(message)   "
            toastLabel.alpha = 1.0
            toastLabel.layer.cornerRadius = 10;
            toastLabel.clipsToBounds  =  true
            toastLabel.font = UIFont(name: (toastLabel.font?.fontName)!, size: 16)
            toastLabel.layoutEdgeInsets.left = 8
            toastLabel.layoutEdgeInsets.right = 8
            toastLabel.center.x = self.view.frame.size.width/2
            self.view.addSubview(toastLabel)
            UIView.animate(withDuration: 5.0, delay: 0.1, options: .curveEaseOut, animations: {
                toastLabel.alpha = 0.0
            }, completion: {(isCompleted) in
                toastLabel.removeFromSuperview()
            })
    }
    

    Space is added with message to provide good spacing at the both ends so that it looks good. Modified version of answer of Mr.Bean

    0 讨论(0)
  • 2020-12-22 19:44
    static func popUp(context ctx: UIViewController, msg: String) {
    
        let toast = UILabel(frame:
            CGRect(x: 16, y: ctx.view.frame.size.height / 2,
                   width: ctx.view.frame.size.width - 32, height: 100))
    
        toast.backgroundColor = UIColor.lightGray
        toast.textColor = UIColor.white
        toast.textAlignment = .center;
        toast.numberOfLines = 3
        toast.font = UIFont.systemFont(ofSize: 20)
        toast.layer.cornerRadius = 12;
        toast.clipsToBounds  =  true
    
        toast.text = msg
    
        ctx.view.addSubview(toast)
    
        UIView.animate(withDuration: 5.0, delay: 0.2,
            options: .curveEaseOut, animations: {
            toast.alpha = 0.0
            }, completion: {(isCompleted) in
                toast.removeFromSuperview()
        })
    }
    

    Then just call it from UIViewController

    popUp(context: self, msg: "Your message")
    
    0 讨论(0)
  • 2020-12-22 19:47

    @mr-bean code updated to latest Swift version (3.x)

        let toastLabel =
            UILabel(frame:
                CGRect(x: self.view.frame.size.width/2 - 150,
                       y: self.view.frame.size.height-100,
                       width: 300,
                       height: 35))
        toastLabel.backgroundColor = UIColor.black
        toastLabel.textColor = UIColor.white
        toastLabel.textAlignment = NSTextAlignment.center
        self.view.addSubview(toastLabel)
        toastLabel.text = message
        toastLabel.alpha = 1.0
        toastLabel.layer.cornerRadius = 10;
        toastLabel.clipsToBounds  =  true
        UIView.animate(withDuration: 4.0, animations: {
            toastLabel.alpha = 0.0
        })
    
    0 讨论(0)
提交回复
热议问题