zoukankan      html  css  js  c++  java
  • 顶层ViewController的获取

    在开发的时候,为了减少耦合,将View进行拆分时,有时候会碰到ViewController上加载另一个ViewController的View。这时我们需要获取当前最上层的ViewController,在最上层的ViewController进行push,present等操作。

    import Foundation
    import UIKit
    
    extension UIApplication {
        
        static func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController)-> UIViewController? {
            
            if let navi = base as? UINavigationController {
                return topViewController(base:navi.visibleViewController)
            }
            
            if let tab = base as? UITabBarController {
                let moreNavigationController = tab.moreNavigationController
                
                if let top = moreNavigationController.topViewController, top.view.window != nil {
                    return topViewController(base:top)
                } else if let selected = tab.selectedViewController {
                    return topViewController(base:selected)
                }
            }
            
            if let presented = base?.presentedViewController {
                return topViewController(base:presented)
            }
            
            return base
        }
    }
    
  • 相关阅读:
    私有 composer 包创建
    随机数是如何生成的
    TCP 三次握手的意义
    何为真何为假
    Python流程控制语句详解
    Python类中装饰器classmethod,staticmethod,property,
    函数进阶
    初识函数
    文件操作
    is ==小数据池编码解码
  • 原文地址:https://www.cnblogs.com/horo/p/6792257.html
Copyright © 2011-2022 走看看