快速检测不同类型的iPad

时间:2018-07-05 08:45:17

标签: ios

这是我的代码。

if UIDevice().userInterfaceIdiom == .phone {
    switch UIScreen.main.nativeBounds.height {
    case 1136:
        print("iPhone 5 or 5S or 5C")

        self.pageControl = UIPageControl(
            frame: CGRect(
                x: 0,
                y: 502 ,
                width: self.view.frame.size.width,
                height: 17
            )
        )
    case 1334:
        print("iPhone 6/6S/7/8")

        self.pageControl = UIPageControl(
            frame: CGRect(
                x: 0,
                y: 601 ,
                width: self.view.frame.size.width,
                height: 17
            )
        )
    case 1920, 2208:
        print("iPhone 6+/6S+/7+/8+")

        self.pageControl = UIPageControl(
            frame: CGRect(
                x: 0,
                y: 670 ,
                width: self.view.frame.size.width,
                height: 17
            )
        )
    case 2436:
        print("iPhone X")
    default:
        print("unknown")
    }
}

if UIDevice().userInterfaceIdiom == .pad {
    switch UIScreen.main.nativeBounds.height {
    case 1024:
        self.pageControl = UIPageControl(
            frame: CGRect(
                x: 0,
                y: 50 ,
                width: self.view.frame.size.width,
                height: 17
            )
        )
    default:
        print("unknown")
    }
}

这里是iphone时,页面控件将按照上面的代码显示。现在我需要在ipad中执行相同的操作。如果发现它是ipad,则将页面控件放在我需要的位置。

但是问题是:-

if UIDevice().userInterfaceIdiom == .pad {
    switch UIScreen.main.nativeBounds.height {
    case 1024:


        self.pageControl = UIPageControl(
            frame: CGRect(
                x: 0,
                y: 50 ,
                width: self.view.frame.size.width,
                height: 17
            )
        )

    default:
        print("unknown")
    }

我在这里需要:-

  • 情况1:如果是ipad 9.7
  • 案例2:ipad 12.9
  • 案例3:ipad Air 2
  • 案例4:ipad Air

那怎么给?

1 个答案:

答案 0 :(得分:0)

根据我们检测到所有设备的情况,这非常容易。

#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == 
UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 
1366)

#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == 
UIUserInterfaceIdiomPad && 
UIScreen.mainScreen.nativeBounds.size.height == 2732)