【发布时间】:2020-02-24 05:00:55
【问题描述】:
每当我在前台收到应用程序的远程推送通知时,我都会尝试显示一个视图。它在 View Controllers 中运行良好,但不适用于表视图控制器。我找不到获取当前可见单元格的 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
}
【问题讨论】:
-
如何在 tableViewController 中显示该视图?给我们看一些代码。
-
我基本上有一个 UIApplication 的扩展,它总是返回给我用户看到的 VC。基于此,我只是将 xib 添加到子视图到负 Y 值并将其设置为 Y = 0。
-
@MarcusRohden 获取该 VC 的导航控制器并将子视图添加到该控制器
标签: ios swift apple-push-notifications tableview xib