How to Toast message in Swift?

后端 未结 22 1010
暖寄归人
暖寄归人 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:32

    For Swift 4

    My version of a Toast that uses layout constraints, with the advantage that it works for any text size, as is (based in the response of Tony Franzis):

    Just call: Toast.show(message: "My message", myViewControllerName)

    class Toast {
        static func show(message: String, controller: UIViewController) {
            let toastContainer = UIView(frame: CGRect())
            toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6)
            toastContainer.alpha = 0.0
            toastContainer.layer.cornerRadius = 25;
            toastContainer.clipsToBounds  =  true
    
            let toastLabel = UILabel(frame: CGRect())
            toastLabel.textColor = UIColor.white
            toastLabel.textAlignment = .center;
            toastLabel.font.withSize(12.0)
            toastLabel.text = message
            toastLabel.clipsToBounds  =  true
            toastLabel.numberOfLines = 0
    
            toastContainer.addSubview(toastLabel)
            controller.view.addSubview(toastContainer)
    
            toastLabel.translatesAutoresizingMaskIntoConstraints = false
            toastContainer.translatesAutoresizingMaskIntoConstraints = false
    
            let a1 = NSLayoutConstraint(item: toastLabel, attribute: .leading, relatedBy: .equal, toItem: toastContainer, attribute: .leading, multiplier: 1, constant: 15)
            let a2 = NSLayoutConstraint(item: toastLabel, attribute: .trailing, relatedBy: .equal, toItem: toastContainer, attribute: .trailing, multiplier: 1, constant: -15)
            let a3 = NSLayoutConstraint(item: toastLabel, attribute: .bottom, relatedBy: .equal, toItem: toastContainer, attribute: .bottom, multiplier: 1, constant: -15)
            let a4 = NSLayoutConstraint(item: toastLabel, attribute: .top, relatedBy: .equal, toItem: toastContainer, attribute: .top, multiplier: 1, constant: 15)
            toastContainer.addConstraints([a1, a2, a3, a4])
    
            let c1 = NSLayoutConstraint(item: toastContainer, attribute: .leading, relatedBy: .equal, toItem: controller.view, attribute: .leading, multiplier: 1, constant: 65)
            let c2 = NSLayoutConstraint(item: toastContainer, attribute: .trailing, relatedBy: .equal, toItem: controller.view, attribute: .trailing, multiplier: 1, constant: -65)
            let c3 = NSLayoutConstraint(item: toastContainer, attribute: .bottom, relatedBy: .equal, toItem: controller.view, attribute: .bottom, multiplier: 1, constant: -75)
            controller.view.addConstraints([c1, c2, c3])
    
            UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
                toastContainer.alpha = 1.0
            }, completion: { _ in
                UIView.animate(withDuration: 0.5, delay: 1.5, options: .curveEaseOut, animations: {
                    toastContainer.alpha = 0.0
                }, completion: {_ in
                    toastContainer.removeFromSuperview()
                })
            })
        }
    }
    
    0 讨论(0)
  • 2020-12-22 19:34

    There is a 3rd party library that supports customizable toast notification with single line of code. Here is a simple example of it:

    import Toast_Swift
    
    ...
    
    // basic usage
    self.view.makeToast("This is a piece of toast")
    
    // toast with a specific duration and position
    self.view.makeToast("This is a piece of toast", duration: 3.0, position: .top)
    

    https://github.com/scalessec/Toast-Swift

    (Updated for Swift 3/4+)

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

    How about use Toaster

    At a Glance

    Toast(text: "Hello, world!").show()
    

    Setting delay and duration

    Toast(text: "Hello, world!", duration: Delay.long)
    Toast(text: "Hello, world!", delay: Delay.short, duration: Delay.long)
    

    Removing Toast

    let toast = Toast(text: "Hello")
    toast.show()
    toast.cancel() // remove toast immediately
    
    

    Customizing Appearance

    • backgroundColor
    • cornerRadius
    • textInsets
    • textColor
    • font
    • bottomOffsetPortrait
    • bottomOffsetLandscape
    • shadowPath
    • shadowColor
    • shadowOpacity
    • shadowOffset
    • shadowRadius
    • maxWidthRatio
    • useSafeAreaForBottomOffset
    0 讨论(0)
  • 2020-12-22 19:38

    Just add below method. This will show message in different colors with animation (message appearing from left to right & disappear).

    Swift 3.0 -

    class Toast
    {
        class private func showAlert(backgroundColor:UIColor, textColor:UIColor, message:String)
        {
    
            let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
            let label = UILabel(frame: CGRect.zero)
            label.textAlignment = NSTextAlignment.center
            label.text = message
            label.font = UIFont(name: "", size: 15)
            label.adjustsFontSizeToFitWidth = true
    
            label.backgroundColor =  backgroundColor //UIColor.whiteColor()
            label.textColor = textColor //TEXT COLOR
    
            label.sizeToFit()
            label.numberOfLines = 4
            label.layer.shadowColor = UIColor.gray.cgColor
            label.layer.shadowOffset = CGSize(width: 4, height: 3)
            label.layer.shadowOpacity = 0.3
            label.frame = CGRect(x: appDelegate.window!.frame.size.width, y: 64, width: appDelegate.window!.frame.size.width, height: 44)
    
            label.alpha = 1
    
            appDelegate.window!.addSubview(label)
    
            var basketTopFrame: CGRect = label.frame;
            basketTopFrame.origin.x = 0;
    
            UIView.animate(withDuration
                :2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in
                    label.frame = basketTopFrame
            },  completion: {
                (value: Bool) in
                UIView.animate(withDuration:2.0, delay: 2.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
                    label.alpha = 0
                },  completion: {
                    (value: Bool) in
                    label.removeFromSuperview()
                })
            })
        }
    
        class func showPositiveMessage(message:String)
        {
            showAlert(backgroundColor: UIColor.green, textColor: UIColor.white, message: message)
        }
        class func showNegativeMessage(message:String)
        {
            showAlert(backgroundColor: UIColor.red, textColor: UIColor.white, message: message)
        }
    }
    
    0 讨论(0)
  • 2020-12-22 19:38

    Swift 4.2 Very easy and super way

    let toastLabel = UILabel()
    
    toastLabel.lineBreakMode = .byWordWrapping
    toastLabel.numberOfLines = 0
    toastLabel.text = "Type your message you want to show in toast"
    toastLabel.sizeToFit()
    //MARK Resize the Label Frame
    toastLabel.frame = CGRect(x: toastLabel.frame.origin.x, y: toastLabel.frame.origin.y, width: toastLabel.frame.size.width + 40, height: toastLabel.frame.size.height + 40)
    self.view.addSubview(toastLabel)
    
    0 讨论(0)
  • 2020-12-22 19:39

    Swift 4

    func showToast(message : String) {
    
        let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
        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
        self.view.addSubview(toastLabel)
        UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
            toastLabel.alpha = 0.0
        }, completion: {(isCompleted) in
            toastLabel.removeFromSuperview()
        })
    }
    

    Call the function like

    self.showToast(message: "Data Save.")
    
    0 讨论(0)
提交回复
热议问题