jdg / MBProgressHUD

MBProgressHUD + Customizations
http://www.bukovinski.com/
MIT License
16.01k stars 3.56k forks source link

Convenience function for flashing NSErrors #330

Closed fulldecent closed 8 years ago

fulldecent commented 8 years ago

Related to #329

Here is the code I use to flash an NSError to the screen. Perhaps this may be added as a class function.

extension MBProgressHUD {
    func flashError(error: NSError) {
        let window = UIApplication.sharedApplication().keyWindow
        MBProgressHUD.hideAllHUDsForView(window, animated: false)

        let hud = MBProgressHUD(forView: window)
        hud.mode = .CustomView
        hud.removeFromSuperViewOnHide = true

        let view: UITextView = UITextView(frame: CGRectMake(0, 0, 200, 200))
        view.text = error.localizedDescription
        view.font = hud.labelFont
        view.textColor = UIColor.whiteColor()
        view.backgroundColor = UIColor.clearColor()
        view.sizeToFit()

        hud.customView = view
        hud.show(true)
        hud.hide(true, afterDelay: 1.2)
    }
}
matej commented 8 years ago

329 has been addressed.