Displaying a message in iOS which has the same functionality as Toast in Android

前端 未结 19 1714
逝去的感伤
逝去的感伤 2020-12-12 12:35

I need to know if there is any method in iOS which behaves like Toast messages in Android. That is, I need to display a message which is dismissed automatically after few se

19条回答
  •  借酒劲吻你
    2020-12-12 13:14

    I thought off a simple way to do the toast! using UIAlertController without button! We use the button text as our message! get it? see below code:

    func alert(title: String?, message: String?, bdy:String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: bdy, style: .Cancel, handler: nil)
        alertController.addAction(okAction)
            self.presentViewController(alertController, animated: true, completion: nil)
    
    
        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            //print("Bye. Lovvy")
            alertController.dismissViewControllerAnimated(true, completion: nil) 
        }    
    }
    

    use it like this:

    self.alert(nil,message:nil,bdy:"Simple Toast!") // toast
    self.alert(nil,message:nil,bdy:"Alert") // alert with "Alert" button
    

提交回复
热议问题