¿Cómo brindar post en Swift?

¿Hay alguna manera de brindar post en swift?

Lo he intentado en el objective c pero no pude encontrar una solución rápida.

[self.view makeToast:@"Account created Successfully" duration:0.5 position:@"bottom"]; 

 extension UIViewController { 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() }) } } 

Simplemente agregue el siguiente método. Esto mostrará el post en diferentes colors con animation (post que aparece de izquierda a derecha y desaparece).

Swift 3.0 –

 class Toast { class private func showAlert(backgroundColor:UIColor, textColor:UIColor, message:String) { let appDelegate: AppDelegate = UIApplication.shanetworking.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.networking, textColor: UIColor.white, message: message) } } 

Lo que necesita exactamente es https://github.com/Rannie/Toast-Swift/blob/master/SwiftToastDemo/Toast/HRToast%2BUIView.swift .

Descargue la class HRToast + UIView.swift y arrastre y suelte para proyectar. Asegúrese de marcar 'copyr elementos si es necesario' en el cuadro de dialog.

  //Usage: self.view.makeToast(message: "Simple Toast") self.view.makeToast(message: "Simple Toast", duration: 2.0, position:HRToastPositionTop) self.view.makeToast(message: "Simple Toast", duration: 2.0, position: HRToastPositionCenter, image: UIImage(named: "ic_120x120")!) self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionDefault, title: "Simple Toast") self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionCenter, title: "Simple Toast", image: UIImage(named: "ic_120x120")!) self.view.makeToastActivity() self.view.makeToastActivity(position: HRToastPositionCenter) self.view.makeToastActivity(position: HRToastPositionDefault, message: "Loading") self.view.makeToastActivityWithMessage(message: "Loading") 

Existe una biblioteca de terceros que admite la notificación de brindis personalizable con una sola línea de código. Aquí hay un simple ejemplo de ello:

 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

(Actualizado para Swift 3)

if makeToast:duration:position: se define en el objective-c y se puede makeToast:duration:position: , entonces el código rápido será

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

Es posible que deba utilizar un encabezado de puente para get acceso a ese método en su código rápido.

@ código mr-bean actualizado a la última versión de Swift (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 })