Тост UILabel появляется за кадром, несмотря на то, что я правильно настроил y - PullRequest
1 голос
/ 12 апреля 2019

У меня есть расширение Toast на ViewController, которое наследует class ViewControllerList: UITableViewController, UIPopoverPresentationControllerDelegate {, и все в порядке, за исключением того, что когда я делаю это

extension UIViewController {

  func showToast(message : String) {
      let height = UIScreen.main.bounds.height
      let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y:  height-100, width: 150, height: 35))
      toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
      toastLabel.textColor = UIColor.white
      toastLabel.textAlignment = .center;
      toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
      toastLabel.text = message
      toastLabel.alpha = 1.0
      toastLabel.layer.cornerRadius = 10;
      toastLabel.clipsToBounds  =  true
      self.view.addSubview(toastLabel)
      UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
          toastLabel.alpha = 0.0
      }, completion: {(isCompleted) in
          toastLabel.removeFromSuperview()
      })
   }
}

, тост ВСЕГДА появляется за кадром ... в длинном представлении ... нетНезависимо от того, как я установил y, он всегда думает с точки зрения документа. Я отладил его и y=676, но все же кажется, что он находится на расстоянии ~ 900 ... 100 от нижней части tableView внутри последней ячейки

почему это так и как это исправить?

Пожалуйста, не отмечайте это - ПОПРОБУЙТЕ дать ответ

Ответы [ 2 ]

1 голос
/ 12 апреля 2019

Вместо добавления к self.view добавьте метку тоста в качестве подпредставления окна:

extension UIViewController {

  func showToast(message : String) {
      let window = (UIApplication.shared.delegate as! AppDelegate).window
      let height = window.bounds.height
      let toastLabel = UILabel(frame: CGRect(x: window.bounds.width/2 - 75, y:  height-100, width: 150, height: 35))
      toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
      toastLabel.textColor = UIColor.white
      toastLabel.textAlignment = .center;
      toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
      toastLabel.text = message
      toastLabel.alpha = 1.0
      toastLabel.layer.cornerRadius = 10;
      toastLabel.clipsToBounds  =  true

      // notice the change here
      window.addSubview(toastLabel)
      UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
          toastLabel.alpha = 0.0
      }, completion: {(isCompleted) in
          toastLabel.removeFromSuperview()
      })
   }
}

Это позволяет избежать проблем, с которыми вам приходится сталкиваться, когда суперпредставление представляет собой прокрутку.

0 голосов
/ 12 апреля 2019

Добавьте новую функцию addToTopView(view : UIView) и используйте как здесь.

extension UIViewController {

    /// adding views as a subview of the top view = window
    func addToTopView(view : UIView) {
        if let d = UIApplication.shared.delegate, let window = d.window! {
            window.addSubview(view)
            // handle constraints or anythings
        }
    }

    func showToast(message : String) {
        let height = UIScreen.main.bounds.height
        let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y:  height-100, width: 150, height: 35))
        toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        toastLabel.textColor = UIColor.white
        toastLabel.textAlignment = .center;
        toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
        toastLabel.text = message
        toastLabel.alpha = 1.0
        toastLabel.layer.cornerRadius = 10;
        toastLabel.clipsToBounds  =  true
        addToTopView(view: toastLabel) // here is update
        UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
            toastLabel.alpha = 0.0
        }, completion: {(isCompleted) in
            toastLabel.removeFromSuperview()
        })
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...