iOS

 
/// 获得当前窗口
var JY_WINDOW: UIWindow? {
    get{
        if let app = UIApplication.shared.delegate as? AppDelegate {
            return app.window
        }
        return nil
    }
}

  

//iPhoneX系列
var iphoneX_Series: Bool {
    get {
        
        if UIDevice.current.userInterfaceIdiom != UIUserInterfaceIdiom.phone{
            debugPrint("不是iPhone, 是 (UIDevice.current.userInterfaceIdiom.rawValue)")
        }
        
        if #available(iOS 11.0, *) {
            if let bottom = JY_WINDOW?.safeAreaInsets.bottom , bottom > 0 {
                return true
            }
        } else {
            debugPrint("iOS11 之前的版本")
        }
        return false
    }
}

  

在iOS11以后的 非iPhoneX系列, 安全区高度是电池蓝高度,一般 获取安全区高度:在非iPhoneX系列 上以 Y = 0 开始, 

var JY_NAV_HEIGHT : CGFloat{
    get{
        if #available(iOS 11.0, *) ,  iphoneX_Series{
            let safeTopHeight = JY_WINDOW?.safeAreaInsets.top ?? 0
            return safeTopHeight
        }
        return 0
    }
}

  

一些基础配置

///获取APP名称
let JY_APP_NAME = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? ""

///获取APP版本号
let JY_APP_VERSION = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
/// 获取手机版本号 let iPhoneSystemVersion: String = UIDevice.current.systemVersion /// 手机机型 var iPhoneModel: String { get { var systemInfo = utsname() uname(&systemInfo) let platform = withUnsafePointer(to: &systemInfo.machine.0) { ptr in return String(cString: ptr) } return platform } } //FIXME:屏幕宽度 let JY_DEVICE_HEIGHT = (UIScreen.main.bounds.size.height) //FIXME:屏幕高度 let JY_DEVICE_WIDTH = (UIScreen.main.bounds.size.width) //FIXME: 判断是不是运行在模拟器上面 struct Platform { static let isSimulator: Bool = { var isSim = false #if arch(i386) || arch(x86_64) isSim = true #endif return isSim }() } //自适应宽高:6S位基准 func fit(_ value:CGFloat) -> CGFloat { return value * (JY_DEVICE_WIDTH < JY_DEVICE_HEIGHT ? JY_DEVICE_WIDTH:JY_DEVICE_HEIGHT ) / 375 } //FIXME: 简写获取nib func GetStoryboardVC(storyboardName:String!,vcName:String!) -> UIViewController { let storeboard:UIStoryboard! = UIStoryboard.init(name: storyboardName, bundle: nil) let vc = storeboard.instantiateViewController(withIdentifier: vcName) return vc }
//FIXME: 自定义debug 模式下不打印
func DDLOG<Message>(message: Message,
              fileName: String = #file,
              methodName: String = #function,
              lineNumber: Int = #line){
    #if DEBUG
        print("((fileName as NSString).pathComponents.last!).(methodName)[(lineNumber)]:(message)")
    #endif
}

  

 

获取当前显示的VC

extension UIViewController {
    
    /// 设置controller的导航标题
    ///
    /// - Parameter title: 标题
    func setNavigationTitleViewTitle(title: String , textColor: UIColor = UIColor.white) {
        let titleLab = JYBaseViewModel.creatLabe(text: title, font: UIFont.systemFont(ofSize: 22, weight: .medium), textColor: textColor , textAlignment: .center)
        titleLab.translatesAutoresizingMaskIntoConstraints = true
        titleLab.bounds = CGRect(x: 0, y: 0,  150, height: 44)
        self.navigationItem.titleView = titleLab
    }
    
    /// 设置导航返回item(controller必须存在导航控制器)
    ///
    /// - Parameter action: 返回的响应事件
    func setNavigationLeftBackItem(action: Selector? = nil, imageName:String = "dateBack") {
        let backBtn = UIButton(fontSize: 17, isSetBoldFontSize: true, textColor: UIColor.init(hexColor: "4A4A4A"), titleStr: "返回", self, action: action, for: .touchUpInside)
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        //  openorder_back_picture
        backBtn.setImage(UIImage.init(named: imageName)?.scaledToSize(newSize: CGSize( 12, height: 20), withScale: false), for: UIControl.State.normal)
        backBtn.bounds = CGRect(origin: CGPoint(x: 0, y: 0), size: backBtn.intrinsicContentSize)
        backBtn.backgroundColor = UIColor.clear
        backBtn.translatesAutoresizingMaskIntoConstraints = true
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
    }
    
    /// 获取当前显示的VC
    ///
    /// - Returns: 当前屏幕显示的VC
    class func getCurrentViewController() -> UIViewController?{
        // 获取当先显示的window
        var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
        if currentWindow.windowLevel != UIWindow.Level.normal {
            let windowArr = UIApplication.shared.windows
            for window in windowArr {
                if window.windowLevel == UIWindow.Level.normal {
                    currentWindow = window
                    break
                }
            }
        }
        return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
    }


//    
    private class func  getNextXController(nextController: UIViewController?) -> UIViewController? {
        if nextController == nil {
            return nil
        }else if nextController?.presentedViewController != nil {
            return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
        }else if let tabbar = nextController as? UITabBarController {
            return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
        }else if let nav = nextController as? UINavigationController {
            return UIViewController.getNextXController(nextController: nav.visibleViewController)
        }
        return nextController
    }
}

  

原文地址:https://www.cnblogs.com/qingzZ/p/9790883.html