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
})
}
}
This is my block of code. However, it never enters the if block. The thing I want to achieve is to not add the view continuously if it already exists there. If the view already exists in the application window,I want it to do nothing. However, it is adding the view everytime the action is being called. I have tried mostly every solution proposed like isdescendantOf(),subviews.contains().. But none has worked so far
这是我的代码块。但是,它永远不会进入if块。我想要实现的是,如果视图已经存在,则不要连续添加视图。如果视图已存在于应用程序窗口中,我希望它不执行任何操作。但是,每次调用操作时都会添加视图。我尝试了大多数提出的每个解决方案,如isdescendantOf(),subviews.contains()..但到目前为止还没有工作
3 个解决方案
#1
2
Change your code to the following
将您的代码更改为以下内容
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)
}
}
Now call your notification from anywhere like
现在可以从任何地方拨打您的通知
CustomNotification.sharedInstance.showNotificationWithTitleWarning()
#2
0
Try checking that toastView
has superview or not. If not , then add it. Following if
condition executes when toastView
has not added on any view.
尝试检查toastView是否具有超级视图。如果没有,则添加它。如果在任何视图上没有添加toastView时执行条件。
if ! toastView.superview
#3
0
You can set a tag to this view and can get "viewWithTag:" if it is returning view that means it is already added
你可以设置一个标签到这个视图,如果它返回视图意味着它已经被添加,可以得到“viewWithTag:”
#1
2
Change your code to the following
将您的代码更改为以下内容
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)
}
}
Now call your notification from anywhere like
现在可以从任何地方拨打您的通知
CustomNotification.sharedInstance.showNotificationWithTitleWarning()
#2
0
Try checking that toastView
has superview or not. If not , then add it. Following if
condition executes when toastView
has not added on any view.
尝试检查toastView是否具有超级视图。如果没有,则添加它。如果在任何视图上没有添加toastView时执行条件。
if ! toastView.superview
#3
0
You can set a tag to this view and can get "viewWithTag:" if it is returning view that means it is already added
你可以设置一个标签到这个视图,如果它返回视图意味着它已经被添加,可以得到“viewWithTag:”