我尝试在前台将应用程序接收到远程推送通知时,尝试显示视图。它在View Controllers中工作正常,但不适用于Table View Controller。我找不到一种方法来获取当前可见单元格的Y位置。
Obs .:如果有人知道如何使其始终显示在导航栏而不是内容视图上,那就更好了。
编辑:遵循一些代码:
//Return the current VC that the user is seeing
func getTopVC()->UIViewController?{
let application = UIApplication.shared
if application.applicationState == .active {
if let topVC = UIApplication.getTopViewController() {
return topVC
}
}
return nil
}
//Animation that shows XIB sliding down from outside the VC to Y = 0
func animateXib(xibToAnimate: ApNotificationsXibView){
let animationTime = 0.3
let originalFrame = xibToAnimate.frame
let options: UIView.AnimationOptions = [.allowUserInteraction]
UIView.animate(withDuration: animationTime, delay: 0, options: options, animations: {
xibToAnimate.frame = CGRect(x: 0, y: 0, width: originalFrame.width, height: originalFrame.height)
}) { (isEntryAnimationDone) in
//SECOND ANIMATION TO SLIDE UP
UIView.animate(withDuration: animationTime, delay: 3.0, animations: {
xibToAnimate.frame = originalFrame
}) { (done) in
xibToAnimate.removeFromSuperview()
}
}
}
//Pre set some values, Add XIB on the current VC SubView and animate it
func displayNotificationXib(sendersName: String, sendersImage: UIImage, textToShow:String, connectionType: ApUserConnection.ConnectionType?, action: ApNotificationsXibView.TypeNotification, originUserId:String){
if(isShowingXIB == false){
if let vcToShowXib = self.getTopVC(){
let xib = self.prepareXibToAdd()
if let connect = ApActiveConnectionsServices.sharedInstance.getLocalConnectionWithId(with: originUserId){
xib.setValues(title: sendersName, sendersImage: sendersImage, body: textToShow, action: action, originId: originUserId, connect: connect)
}
vcToShowXib.view.addSubview(xib)
animateXib(xibToAnimate: xib)
}
}
}
//EXTENSION of UIApplication that get the current VC that the user is seeing
class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)
} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)
} else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}
如果要在导航栏上显示自定义视图,请将视图添加到导航控制器,而不是将其添加到视图控制器或表视图控制器。
let customView = //...
self.navigationController?.view.addSubview(customView)