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

前端 未结 19 1715
逝去的感伤
逝去的感伤 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:23

    Swift 3

    For a simple solution without third party code:

    Just use a normal UIAlertController but with style = actionSheet (look at code down below)

    let alertDisapperTimeInSeconds = 2.0
    let alert = UIAlertController(title: nil, message: "Toast!", preferredStyle: .actionSheet)
    self.present(alert, animated: true)
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + alertDisapperTimeInSeconds) {
      alert.dismiss(animated: true)
    }
    

    The advantage of this solution:

    1. Android like Toast message
    2. Still iOS Look&Feel

提交回复
热议问题