尽管我正确设置了y,但Toast UILabel出现在屏幕外

时间:2019-04-12 10:23:38

标签: swift uitableview uilabel

我在继承class ViewControllerList: UITableViewController, UIPopoverPresentationControllerDelegate {的ViewController上有一个Toast扩展,一切正常,除了我这样做

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()
      })
   }
}

吐司面包总是出现在屏幕上..在较长的uiableview中..不管我如何设置y,它总是以文档的方式思考。在最后一个单元格内位于tableView底部约900〜100 ... 100左右的位置

这是为什么,如何解决?

请不要将其标记为-尝试给出答案

2 个答案:

答案 0 :(得分:1)

而不是添加到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()
      })
   }
}

这避免了当超级视图是滚动视图时必须解决的问题。

答案 1 :(得分:0)

添加新功能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()
        })
    }
}