How to Toast message in Swift?

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

提交回复
热议问题