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