美文网首页Swift开发iOS学习笔记iOS进阶指南
iOS开发 判断不同屏幕尺寸 DeviceSzie

iOS开发 判断不同屏幕尺寸 DeviceSzie

作者: 小黑Swift | 来源:发表于2016-04-05 12:31 被阅读2053次

    简单封装通过获取初始化屏幕的大小进行判断不同设备尺寸,方便纯代码布局 UI 或进行不同处理时候使用。

    DeviceSzie.swift
    import UIKit
    struct DeviceSzie {
        
        enum DeviceType {
            case iphone4
            case iphone5
            case iphone6
            case iphone6p
        }
        
        //判断屏幕类型
        static func currentSize() -> DeviceType {
            let screenWidth = UIScreen.mainScreen().bounds.width
            let screenHeight = UIScreen.mainScreen().bounds.height
            
            switch (screenWidth, screenHeight) {
            case (320, 480),(480, 320):
                return .iphone4
            case (320, 568),(568, 320):
                return .iphone5
            case (375, 667),(667, 375):
                return .iphone6
            case (414, 736),(736, 414):
                return .iphone6p
            default:
                return .iphone6
            }
        }
    }
    

    使用:
    ViewController.swift

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 使用
            let currDevice = DeviceSzie.currentSize()
            /**
            * 单事件处理
            */
            // 方式一
            if case .iphone4 = currDevice {
                print("曾几何时,想拥有一部iphone4")
            }
            // 方式二
            if currDevice == .iphone6 {
                print("哥还没有iphone6")
            }
            
            /**
            * 分情况处理
            */
            switch currDevice {
            case .iphone4:
                print("3.5寸屏幕")
            case .iphone5:
                print("4寸屏幕")
            case .iphone6:
                print("4.7寸屏幕")
            case .iphone6p:
                print("5.5寸屏幕")
            }
        }
    }

    相关文章

      网友评论

        本文标题:iOS开发 判断不同屏幕尺寸 DeviceSzie

        本文链接:https://www.haomeiwen.com/subject/hcsplttx.html