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()..但没有工作到目前为止如何检查我的应用程序窗口中是否已存在视图?
尝试使它成为一个实例函数,并使吐司成为一个实例var,并删除i t再从子视图再加入子视图然后 – Shubhank
请问你可以发布一些源代码来做到这一点吗? –
你能说出这个函数在哪个类中吗? – Shubhank