class func showNotificationWithTitleWarning(controller:UIViewController,title :String, subtitle :String){
let subtitle = subtitle
var isTrue : Bool = false
//let horizontalPadding:CGFloat = 0.0
let deviceWidth:CGFloat = Device.DeviceWidth
//let deviceHeight:CGFloat = Device.DeviceHeight
let height:CGFloat = 64.0
let contentFrame:CGRect = CGRectMake(0,0 , deviceWidth ,height)
var toastView:CustomTopNotification!
toastView = CustomTopNotification(frame:contentFrame,Str_title:subtitle)
if toastView.superview === UIApplication.sharedApplication().delegate?.window!! {
toastView.removeFromSuperview()
print("Already there")
} else {
UIApplication.sharedApplication().delegate?.window!!.addSubview(toastView)
toastView.frame.origin.y = -80
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
toastView.frame.origin.y = 0
controller.view.layoutIfNeeded()
},completion: {_ in
})
}
}
这是我的代码块。但是,它永远不会进入if块。我要实现的目标是,如果视图已经存在,则不要连续添加它。如果该视图已经存在于应用程序窗口中,我希望它什么也不做。但是,每次调用动作时都会添加视图。我尝试了几乎所有提议的解决方案,例如isdescendantOf(),subviews.contains()。
最佳答案
将您的代码更改为以下内容
class CustomNotification {
static let sharedInstance = CustomNotification()
private var toastView: CustomTopNotification?
func showNotificationWithTitleWarning(controller:UIViewController,title :String, subtitle :String){
if let prevToastView = toastView {
prevToastView.removeFromSuperView()
}
//prev code of function here
// change only one line in it
toastView:CustomTopNotification!
toastView = CustomTopNotification(frame:contentFrame,Str_title:subtitle)
}
}
现在从任何地方拨打您的通知
CustomNotification.sharedInstance.showNotificationWithTitleWarning()
关于ios - 如何检查我的应用程序窗口中是否已经存在 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37250706/