programing

장치 너비 및 높이를 가져오는 방법

kingscode 2023. 8. 29. 21:39
반응형

장치 너비 및 높이를 가져오는 방법

목표-C에서는 다음 코드를 사용하여 장치 폭과 높이를 얻을 수 있습니다.

CGRect sizeRect = [UIScreen mainScreen].applicationFrame
float width = sizeRect.size.width
float height = sizeRect.size.height

스위프트와 어떻게 이런 일을 할 수 있습니까?

시도해 본 적은 없지만 그래야 할 것 같습니다.

var bounds = UIScreen.main.bounds
var width = bounds.size.width
var height = bounds.size.height

스위프트 4.2

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height

당신의 코드에서 사용하고 싶다면요.여기 있어요.

func iPhoneScreenSizes() {
    let bounds = UIScreen.main.bounds
    let height = bounds.size.height

    switch height {
    case 480.0:
        print("iPhone 3,4")
    case 568.0:
        print("iPhone 5")
    case 667.0:
        print("iPhone 6")
    case 736.0:
        print("iPhone 6+")
    case 812.0:
        print("iPhone X")
        print("iPhone XS")
        break
    case 896.0:
        print("iPhone XR")
        print("iPhone XS Max")
        break
    default:
        print("not an iPhone")

    }
}

@Houssni의 대답은 맞지만 Swift에 대해 이야기하고 있고 이 사용 사례가 자주 나올 것이기 때문에 연장을 고려할 수 있습니다.CGRect다음과 유사합니다.

extension CGRect {
    var wh: (w: CGFloat, h: CGFloat) {
        return (size.width, size.height)
    }
}

그런 다음 다음과 같이 사용할 수 있습니다.

let (width, height) = UIScreen.mainScreen().applicationFrame.wh

만세! :)

(Swift 3) 대부분의 너비 및 높이 값은 장치의 현재 방향을 기준으로 합니다.회전을 기반으로 하지 않고 세로 방향 회전에 있는 것처럼 결과를 제공하는 일관된 값을 원한다면 고정 좌표 공간을 사용해 보십시오.

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds
var sizeRect = UIScreen.mainScreen().applicationFrame
var width    = sizeRect.size.width
var height   = sizeRect.size.height

바로 이와 같이, 그것도 테스트했습니다.

장치 화면 크기를 찾고 있으므로 가장 간단한 방법은 다음과 같습니다.

let screenSize = UIScreen.mainScreen().bounds.size
let width = screenSize.width
let height = screenSize.height

Xcode 12에 적합합니다.

func iPhoneScreenSizes() {
        let height = UIScreen.main.bounds.size.height
        switch height {
        case 480.0:
            print("iPhone 3,4")
        case 568.0:
            print("iPhone 5 | iPod touch(7th gen)")
        case 667.0:
            print("iPhone 6 | iPhone SE(2nd gen) | iPhone 8")
        case 736.0:
            print("iPhone 6+ | iPhone 8+")
        case 812.0:
            print("iPhone X | iPhone XS | iPhone 11 Pro")
        case 896.0:
            print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
        default:
            print("not an iPhone")
        }
    }

UIS 화면 개체는 하드웨어 기반 디스플레이와 관련된 속성을 정의합니다. iOS 장치에는 기본 화면과 0개 이상의 연결된 화면이 있습니다.각 화면 개체는 관련 디스플레이 및 기타 관심 있는 속성에 대한 경계 사각형을 정의합니다.

Apple Doc URL:

https://developer.apple.com/reference/uikit/uiwindow/1621597-screen

빠른 3.0으로 사용자 장치의 높이/폭을 얻으려면

let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width

다음은 사용 가능한 기능 내의 크기에 대한 업데이트된 목록입니다.

func iScreenSizes() {
            let height = UIScreen.main.bounds.size.height
            print("Device height: \(height)")
            switch height {
            case 480.0:
                print("iPhone 3 | iPhone 4 | iPhone 4S")
            case 568.0:
                print("iPhone 5 | iPhone 5S | iPhone 5C | iPhone SE")
            case 667.0:
                print("iPhone 6 | iPhone 7 | iPhone 8 | iPhone SE(2nd gen)")
            case 736.0:
                print("iPhone 6+ | iPhone 7+ | iPhone 8+")
            case 780.0:
                print("iPhone 12 Mini")
            case 812.0:
                print("iPhone X | iPhone XS | iPhone 11 Pro")
            case 844.0:
                print("iPhone 12 | iPhone 12 Pro")
            case 896.0:
                print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
            case 926.0:
                print("iPhone 12 Pro Max")
            case 1024.0:
                print("iPad 1st gen | iPad 2 | iPad 3rd gen | iPad mini | iPad 4th gen | iPad Air | iPad mini 2 | iPad mini 3 | iPad Air 2 | iPad mini 4 | iPad 5th gen | iPad 6th gen | iPad  mini 5")
            case 1112.0:
                print("iPad Pro 2nd gen 10.5'' | iPad Air 3")
            case 1194.0:
                print("iPad Pro 3rd gen 11.0'' | iPad Pro 4th gen 11.0''")
            case 1366.0:
                print("iPad Pro 1st gen 12.9'' | iPad 2nd gen 12.9'' | iPad 3rd gen 12.9'' | iPad Pro 4th gen 12.9''")
            default:
                print("not listed in function")
            }
        }

@Adam Smaka의 대답은 가까웠지만 Swift 3에서는 다음과 같습니다.

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height
 static func getDeviceType() -> String
    {
        var strDeviceType = ""
        if UIDevice().userInterfaceIdiom == .phone {
            switch UIScreen.main.nativeBounds.height {
            case 1136:
                strDeviceType = "iPhone 5 or 5S or 5C"
            case 1334:
                strDeviceType = "iPhone 6/6S/7/8"
            case 1920, 2208:
                strDeviceType = "iPhone 6+/6S+/7+/8+"
            case 2436:
                strDeviceType = "iPhone X"
            case 2688:
                strDeviceType = "iPhone Xs Max"
            case 1792:
                strDeviceType = "iPhone Xr"
            default:
                strDeviceType = "unknown"
            }
        }
        return strDeviceType
    }

let bounds = UIScreen.main.bounds

Swift 4에서는 NSScreen.main?을 사용해야 했습니다.장치설명

let deviceDescription = NSScreen.main?.deviceDescription          
let screenSize = deviceDescription![.size]
let screenHeight = (screenSize as! NSSize).height

UIScreen.main.boundsiOS의 미래 버전에서는 더 이상 사용되지 않습니다.view.window.windowScene.screen

 let height = view.window?.windowScene?.screen.bounds.height ?? 0
 let width = view.window?.windowScene?.screen.bounds.width ?? 0

언급URL : https://stackoverflow.com/questions/24084941/how-to-get-device-width-and-height

반응형