简体   繁体   中英

Detect the different types of ipad in swift

This is my code.

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")
    }
}

Here when it is iphone the page control will display according the code above.Now i need to do same in ipad.If it is found that it is ipad then place the page control on location which i needed.

But the problem is :-

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")
    }

Here i need in such way that :-

  • case 1:if it is ipad 9.7
  • case 2:ipad 12.9
  • case 3:ipad Air 2
  • case 4:ipad Air

So how to give?

It's very easy according to hight we detect all the device.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM