Skip to content

Instantly share code, notes, and snippets.

@ArchieGoodwin
Last active May 23, 2016 18:58
Show Gist options
  • Save ArchieGoodwin/d07895ac1481d20af509ec08c93a6119 to your computer and use it in GitHub Desktop.
Save ArchieGoodwin/d07895ac1481d20af509ec08c93a6119 to your computer and use it in GitHub Desktop.
Shows message for given time in upper part of screen above all windows including status bar (swift)
func showMessage(message : String, delay : NSTimeInterval, color : UIColor, completion: boolClosure?)
{
dispatch_async(self.GlobalMainQueue)
{
guard let window = appDelegate.window else
{
completion!(result: false,error: nil)
return
}
guard let rootViewController = window.rootViewController else
{
completion!(result: false,error: nil)
return
}
UIApplication.sharedApplication().delegate?.window!!.windowLevel = UIWindowLevelStatusBar + 1
let view = UIView(frame: CGRectMake(0, -50, UIScreen.mainScreen().bounds.width, 50))
view.backgroundColor = color
view.tag = 8999
let lbl = MLabel(frame: CGRectMake(5, 0, UIScreen.mainScreen().bounds.width - 10, 50))
lbl.text = message
lbl.font = UIFont(name: "Open Sans Semibold", size: 12)
lbl.numberOfLines = 0
lbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
lbl.textColor = UIColor.whiteColor()
lbl.textAlignment = NSTextAlignment.Center
view.addSubview(lbl)
rootViewController.view.addSubview(view)
UIView.animateWithDuration(0.4, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
var frame = view.frame
frame.origin.y = 0
view.frame = frame
rootViewController.view.layoutSubviews()
}) { (success) -> Void in
UIView.animateWithDuration(0.3, delay: delay, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
var frame = view.frame
frame.origin.y = -50
view.frame = frame
rootViewController.view.layoutSubviews()
}) { (success) -> Void in
rootViewController.view.viewWithTag(8999)?.removeFromSuperview()
UIApplication.sharedApplication().delegate?.window!!.windowLevel = UIWindowLevelNormal
if(completion != nil)
{
completion!(result: true,error: nil)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment