简体   繁体   English

获取最高的 UIViewController(如果以模态方式呈现)

[英]Get top most UIViewController if its presented modally

I have presented a ViewController modally using vc.modalPresentationStyle =.fullScreen我已经使用vc.modalPresentationStyle =.fullScreen模态地呈现了一个 ViewController

But when I try to get the top most view controller using the below code, it returns nil.但是,当我尝试使用下面的代码获取最高视图 controller 时,它返回 nil。

func getTopViewController() -> UIViewController? {
    var topController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
    while topController?.presentedViewController != nil {
        topController = topController?.presentedViewController
    }
    return topController
}

Here topController?.presentedViewController returns nil, if I present it using vc.modalPresentationStyle =.fullScreen and its working fine if I use default presentation style.如果我使用vc.modalPresentationStyle =.fullScreen呈现它, topController?.presentedViewController将返回 nil,如果我使用默认呈现样式,它的工作正常。

Did anyone faced the same issue, any help would be appreciated.有没有人遇到同样的问题,任何帮助将不胜感激。

I have created this extension for getting topmost controller我已经创建了这个扩展以获得最顶层的 controller

   extension UIViewController {
    var topMostViewController : UIViewController {
        
        if let presented = self.presentedViewController {
            return presented.topMostViewController
        }
        
        if let navigation = self as? UINavigationController {
            return navigation.visibleViewController?.topMostViewController ?? navigation
        }
        
        if let tab = self as? UITabBarController {
            return tab.selectedViewController?.topMostViewController ?? tab
        }
        return self
    }
}

extension UIApplication {
    var topMostViewController : UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController
    }
}

Hope this will work for you希望这对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 呈现的UIViewController模态显示移到顶部 - UIViewController presented modally show up shifted to the top 确定是否以模态方式呈现UIViewController - Determining if a UIViewController is being presented modally 模态呈现的UIViewController在关闭时不会清空内存 - Modally presented UIViewController does not empty memory on close 以模态呈现的UIViewController的背景颜色随机变化 - Background Color Randomly Changes of UIViewController presented Modally 在iOS7上模态显示时,以前的UIViewController会流血 - Previous UIViewController bleeds through when presented modally on iOS7 使用选项UIModalPresentationOverCurrentContext减少以模态方式呈现的uiviewcontroller的帧大小 - Reducing the frame size of a uiviewcontroller being presented modally with option UIModalPresentationOverCurrentContext 有没有办法知道UIViewController是否已经以模态方式呈现和解散? - Is there a way to know if a UIViewController has been presented and dismissed modally ? 在以模态显示的UIViewController中使用UITableView时的奇怪行为 - Odd behaviour when using UITableView in a UIViewController that is presented Modally `SwiftUI` - `EnvironmentObject`s,`EnvironmentValues` 不会传播到模态呈现的 `UIViewController` - `SwiftUI` - `EnvironmentObject`s, `EnvironmentValues` does not propagate to modally presented `UIViewController` 如何以模态方式呈现的UIViewController的UIView总是出现在屏幕的底部? - How to have a UIView of a UIViewController that will be presented modally always be present at the bottom of the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM