Editar: Actualización de Swift 3
Aquí hay una versión de Swift 3 basada en wojciech_m la respuesta de aciejewski. Esto se parece más a Android Toast y no acumula tostadas entre sí. Dibuja tostadas en el centro de la pantalla. Puede manejar textos largos de líneas múltiples.
import UIKit
class ToastView: UIView {
private static let hLabelGap: CGFloat = 40.0
private static let vLabelGap: CGFloat = 20.0
private static let hToastGap: CGFloat = 20.0
private static let vToastGap: CGFloat = 10.0
private var textLabel: UILabel!
static func showInParent(_ parentView: UIView, _ text: String, duration: Double = 3.0) {
let labelFrame = CGRect(x: parentView.frame.origin.x + hLabelGap,
y: parentView.frame.origin.y + vLabelGap,
width: parentView.frame.width - 2 * hLabelGap,
height: parentView.frame.height - 2 * vLabelGap)
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 15.0)
label.text = text
label.backgroundColor = UIColor.clear
label.textAlignment = NSTextAlignment.center
label.textColor = UIColor.white
label.numberOfLines = 0
label.frame = labelFrame
label.sizeToFit()
let toast = ToastView()
toast.textLabel = label
toast.addSubview(label)
toast.frame = CGRect(x: label.frame.origin.x - hToastGap,
y: label.frame.origin.y - vToastGap,
width: label.frame.width + 2 * hToastGap,
height: label.frame.height + 2 * vToastGap)
toast.backgroundColor = UIColor.darkGray
toast.alpha = 0.0
toast.layer.cornerRadius = 20.0
toast.center = parentView.center
label.center = CGPoint(x: toast.frame.size.width/2, y: toast.frame.size.height/2)
parentView.addSubview(toast)
UIView.animate(withDuration: 0.4, animations: {
toast.alpha = 0.9
label.alpha = 0.9
})
toast.perform(#selector(hideSelf), with: nil, afterDelay: duration)
}
@objc private func hideSelf() {
UIView.animate(withDuration: 0.4, animations: {
self.alpha = 0.0
self.textLabel.alpha = 0.0
}, completion: { t in self.removeFromSuperview() })
}
}
Uso de otro controlador:
ToastView.showInParent(navigationController!.view, "Hello world")
teniendo en cuenta que sólo con iOS 4 aplicaciones de fondo permitidos , no había una necesidad previa para tal característica. Sin duda alguien hará una, pero no estoy familiarizado con nada parecido. –
@StephenFurlani, aunque no es obvio de inmediato, los mensajes de brindis también son útiles y ampliamente utilizados en aplicaciones de primer plano. –
@DanielS. De todo corazón estoy de acuerdo. Sin embargo, el OP tiene casi 4 años. En este punto hay algunas características EXCELENTES con iOS 8 con respecto a 'UIPopoverPresentationController's y' passthroughViews' –