关于C#:如何在swift中以编程方式检查iphone 4和iphone 5的屏幕尺寸

how to check screen size of iphone 4 and iphone 5 programmatically in swift

我要把目标C换成斯威夫特。有人对如何转移它有什么建议吗?

1
2
3
4
5
6
7
8
9
10
11
12
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone Classic
    }
    if(result.height == 568)
    {
        // iPhone 5
    }
}


XCode 9?SWIFT 4

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
extension UIDevice {
    var iPhoneX: Bool {
        return UIScreen.main.nativeBounds.height == 2436
    }
    var iPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
    }
    enum ScreenType: String {
        case iPhones_4_4S ="iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE ="iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 ="iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus ="iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhones_X_XS ="iPhone X or iPhone XS"
        case iPhone_XR ="iPhone XR"
        case iPhone_XSMax ="iPhone XS Max"
        case unknown
    }
    var screenType: ScreenType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhones_4_4S
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1792:
            return .iPhone_XR
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhones_X_XS
        case 2688:
            return .iPhone_XSMax
        default:
            return .unknown
        }
    }
}

ZZU1


Alternative solution with uiscreen extension(IOS 8 and later):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
extension UIScreen {

    enum SizeType: CGFloat {
        case Unknown = 0.0
        case iPhone4 = 960.0
        case iPhone5 = 1136.0
        case iPhone6 = 1334.0
        case iPhone6Plus = 1920.0
    }

    var sizeType: SizeType {
        let height = nativeBounds.height
        guard let sizeType = SizeType(rawValue: height) else { return .Unknown }
        return sizeType
    }
}

惯例:

1
2
3
if UIScreen.mainScreen().sizeType == .iPhone4 {
    // Make specific layout for small devices.
}


1
2
3
4
5
6
7
8
9
10
if UIDevice().userInterfaceIdiom == .Phone {
    switch UIScreen.mainScreen().bounds.size.height{
    case 480:
        print("iPhone 4S")
    case 568:
        print("iPhone 5")
    default:
        print("other models")
    }
}

Uiscreen.Mainscreen().Bounds.size.hight will provide the hight of the device.使用这个值,我们可以检查特定的iphone设备。


用iphone xs,iphone xs最大值

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
extension UIDevice {
    var iPhoneX: Bool {
        return UIScreen.main.nativeBounds.height == 2436
    }
    var iPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
    }
    enum ScreenType: String {
        case iPhone4_4S ="iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE ="iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 ="iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus ="iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhoneXR ="iPhone XR"
        case iPhoneX_iPhoneXS ="iPhone X,iPhoneXS"
        case iPhoneXSMax ="iPhoneXS Max"
        case unknown
    }
    var screenType: ScreenType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4_4S
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1792:
            return .iPhoneXR
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhoneX_iPhoneXS
        case 2688:
            return .iPhoneXSMax
        default:
            return .unknown
        }
    }
}

Print("screentype:",uidevice.current.screentype.rawvalue)


Just to add to https://stackoverflow.com/A/27776671/1766242

允许你写作

1
2
3
4
5
if UIDevice.isScreen35inch() {
    // Do something to fit small screen
} else {
    // do something else for larger screens
}

See below

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
extension UIDevice {
var iPhone: Bool {
    return UIDevice().userInterfaceIdiom == .Phone
}
enum ScreenType: String {
    case iPhone4
    case iPhone5
    case iPhone6
    case iPhone6Plus
    case Unknown
}
var screenType: ScreenType? {
    guard iPhone else { return nil }
    switch UIScreen.mainScreen().nativeBounds.height {
    case 960:
        return .iPhone4
    case 1136:
        return .iPhone5
    case 1334:
        return .iPhone6
    case 2208:
        return .iPhone6Plus
    default:
        return nil
    }
}

// helper funcs
static func isScreen35inch() -> Bool {
    return UIDevice().screenType == .iPhone4
}

func isScreen4inch() -> Bool {
    return UIDevice().screenType == .iPhone5
}

func isScreen47inch() -> Bool {
    return UIDevice().screenType == .iPhone6
}

func isScreen55inch() -> Bool {
    return UIDevice().screenType == .iPhone6Plus
}}

这是我用XCode 10+Swift 4+IPhone XS/XS/XS/XR做的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct ScreenSize {
    static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType {
    static let IS_IPHONE_4_OR_LESS  = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPHONE_X          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812
    static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH   == 1024.0
    static let IS_IPAD_PRO          = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH   == 1366.0
    static let IS_IPHONE_XSMAX      = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 2688
    static let IS_IPHONE_XR         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 1792
}


另外,在建议的方式中,一种方法是沿着同样的方式使用结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct ScreenSize
{
  static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
  static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
  static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
  static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH,    ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
  static let IS_IPHONE_4_OR_LESS  = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
  static let IS_IPHONE_5          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
  static let IS_IPHONE_6          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
  static let IS_IPHONE_6P         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
  static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH   == 1024.0
  static let IS_IPAD_PRO          = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH   == 1366.0
}

让我知道它是否有帮助。


李奥为我固定了答案。这里是整个Xamarin.ios devs:)

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
public static class ScreenHelper
{
    public enum ScreenSizeType
    {
        iPhone4_4s,
        iPhone5_5s_5c_SE,
        iPhone6_6s_7_8,
        iPhone6plus_6sPlus_7Plus_8Plus,
        iPhoneX,
        unknown
    }

    public static bool IsIphone
    {
        get
        {
            return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone;
        }
    }

    public static bool IsNarrowScreen
    {
        get
        {
            return UIScreen.MainScreen.NativeBounds.Width <= 640;
        }
    }

    public static ScreenSizeType CurrentScreenType
    {
        get
        {
            switch ((int)UIScreen.MainScreen.NativeBounds.Height)
            {
                case 960:
                    return ScreenSizeType.iPhone4_4s;
                case 1136:
                    return ScreenSizeType.iPhone5_5s_5c_SE;
                case 1334:
                    return ScreenSizeType.iPhone6_6s_7_8;
                case 1920:
                case 2208:
                    return ScreenSizeType.iPhone6plus_6sPlus_7Plus_8Plus;
                case 2436:
                    return ScreenSizeType.iPhoneX;
                default:
                    return ScreenSizeType.unknown;
            }
        }
    }
}

Try to use this:

创建UiviewController+Extension.swift file:and put into below code:

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
enum DeviceTypeModel{
    case iphoneX
    case iphone8Plus
    case iphone8
    case iphoneSE //SE is the like iphone 5 and iphone 5s
    case iphone4s
}

extension UIViewController{
    func runOnTheDeviceType(_ completion: (DeviceTypeModel) -> Void) {
        if UIDevice().userInterfaceIdiom == .phone{
            switch UIScreen.main.nativeBounds.height{
            case 2436:
                completion(.iphoneX)
            case 1920:
                completion(.iphone8Plus)
            case 1334:
                completion(.iphone8)
            case 1136:
                completion(.iphoneSE)
            default:
                completion(.iphone4s)
            }
        }
    }
}

并在你的视图控制器中使用它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ChatViewController: UIViewController{
      override func viewDidLoad() {
        super.viewDidLoad()
          runOnTheDeviceType { devices in
            switch devices{
            case .iphoneSE:
                //do something
            case .iphone4s:
                //do something
            default:
                //do something
            }
        }
      }
}

如果你想做的话,你可以添加更多的例子。享受它。


你也可以试试下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
 if (IS_IPHONE4) {
        imgHeight.constant = 150;
    }
    else if (IS_IPAD) {
        imgHeight.constant = 300;
    }
    else if (IS_IPHONE5) {
        imgHeight.constant = 170;
    }
    else {
        imgHeight.constant = 200;
    }

谢谢