0

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?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
meggie
  • 5
  • 6
  • Possible duplicate of [How to determine the current iPhone/device model?](https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model) – Alec. Jul 05 '18 at 08:52
  • 1
    Is this purely for positioning the PageControl? If so, why not use the SafeArea for sizes, that way you are are not device dependant? Use the height of the SafeArea to calculate a position. Or use Storyboards and add a constraint – Flexicoder Jul 05 '18 at 08:53
  • @Flexicoder i am developing the app without using main storyboard,and my pagecontrol is coding .so how to solve – meggie Jul 05 '18 at 09:02
  • 1
    You can still use autolayout without storyboards. Coding for specific device dimensions is generally a bad idea as you will need to keep adjusting for new devices when Apple releases them – Paulw11 Jul 05 '18 at 09:06
  • 1
    Using hardcoded device sizes is a terrible idea. What happens if the Apple release a new device with a different resolution? What happens if your app is run in split screen on the iPad? And you shouldn't just be setting the frame of a subview - you should use layout constraints. – Ashley Mills Jul 05 '18 at 10:51
  • 1
    As a practical example of why this is not a good idea: how will you lay out your screen if the iPad is in side-by-side mode? Or slide over? Assuming that any device is of a particular size is extremely difficult to make work now and, as others have already noted, is not future proof. – Stephen Darlington Jul 05 '18 at 10:54

1 Answers1

1

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)
SAXENA
  • 423
  • 1
  • 4
  • 17