尽管我正确设置了y,但Toast UILabel出现在屏幕外
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尽管我正确设置了y,但Toast UILabel出现在屏幕外相关的知识,希望对你有一定的参考价值。
我在ViewController上有一个继承class ViewControllerList: UITableViewController, UIPopoverPresentationControllerDelegate {
的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()
})
}
}
toast ALWAYS出现在屏幕上..在一个很长的uitableview ..无论我如何设置y
它总是在文件方面思考..我已调试它和y=676
但它似乎关于放置在约900 ... 100关闭tableView的底部就在最后一个单元格的里面
为什么这样,我该如何解决?
请不要将此标记下来 - 尝试给出答案
答案
而不是添加到self.view
,添加您的toast标签作为窗口的子视图:
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()
})
}
}
这避免了当超级视图是滚动视图时必须处理的问题。
另一答案
添加一个新功能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()
})
}
}
以上是关于尽管我正确设置了y,但Toast UILabel出现在屏幕外的主要内容,如果未能解决你的问题,请参考以下文章
尽管正确打印出所有输出,但程序未在 Java 中以退出代码 0 结束
UITableViewCell 计算正确的高度,但 UILabel 不换行