随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万
 

 

1
2
3
4
5
6
7
8
9
/// 获得当前窗口
var JY_WINDOW: UIWindow? {
    get{
        if let app = UIApplication.shared.delegate as? AppDelegate {
            return app.window
        }
        return nil
    }
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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 开始, 

1
2
3
4
5
6
7
8
9
var JY_NAV_HEIGHT : CGFloat{
    get{
        if #available(iOS 11.0, *) ,  iphoneX_Series{
            let safeTopHeight = JY_WINDOW?.safeAreaInsets.top ?? 0
            return safeTopHeight
        }
        return 0
    }
}

  

一些基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
///获取APP名称
let JY_APP_NAME = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? ""
 
///获取APP版本号
let JY_APP_VERSION = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""<br>
/// 获取手机版本号
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
}

 

1
//FIXME: 自定义debug 模式下不打印
1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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, width: 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(width: 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
    }
}

  

posted on   懂事长qingzZ  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示