-1

my app got rejected because it does not look ok on iPhone 4s and on iPad in iPhone resolution. I have some buttons that overlap some fields on that resolution. The problem is that now I've looked into XCode and I see that all non plus iPhone models have the same size classes. How could I make difference between them? I add those buttons in code. The other fields are added in IB.

EDIT: Those buttons added in code, are positioned with absolute coordinates, the fields added in IB uses auto layout.

kemkriszt
  • 280
  • 3
  • 17
  • are you using auto layout? – Shamas S May 30 '17 at 10:47
  • Possible duplicate of [how to check screen size of iphone 4 and iphone 5 programmatically in swift](https://stackoverflow.com/questions/27775779/how-to-check-screen-size-of-iphone-4-and-iphone-5-programmatically-in-swift) – shallowThought May 30 '17 at 10:49
  • Maybe, but I'm not sure it would solve the problem with iPads. – kemkriszt May 30 '17 at 10:50
  • You can't - easily anyways - mix auto layout and frames. Why are you not using auto layout in code? –  May 30 '17 at 11:04

3 Answers3

0

You can distinguish different devices based on height UIScreen.main.bounds.height <= HeightConstant.iPhone4sHeight

height value of phones

static let iPhone5sHeight: CGFloat = 568 static let iPhone4sHeight: CGFloat = 480

anujsyal
  • 168
  • 1
  • 6
0

Generally I would not use storyboard :P but for this case, since you are using storyboard, you can use traits. The traits will let you design multiple design for the same controller using size, orientation or device.

http://www.techotopia.com/index.php/Using_Trait_Variations_to_Design_Adaptive_iOS_User_Interfaces

It's the cleanest solution without any code.

teixeiras
  • 101
  • 8
0

Though i am a bit late and i faced the exact problem before.However.

Step 1:

Take a base screen for design. I choose iphone 7 plus.

Step 2:

Define some constants.

Why?

We need to multiply it with the autolayout constraint's constant.

Why auto-layout?

If you want to adapt your app in all of the devices, you must use auto-layout and size classes.

I am giving you my constant:

var SCREEN_WIDTH = UIScreen.main.bounds.size.width
var SCREEN_HEIGHT = UIScreen.main.bounds.size.height
var BASE_SCREEN_HEIGHT:CGFloat = 736.0
var SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT)
var ASPECT_RATIO_RESPECT_OF_7P = SCREEN_MAX_LENGTH / BASE_SCREEN_HEIGHT

Step 3:

You are done. Now how to use this. Suppose you did auto-layout to a button which has leading constant 30

in viewDidLoad

just write:

yourButtonLeadingConstraint.constant = 30 * ASPECT_RATIO_RESPECT_OF_7P

Only for this line it will maintain 30Px aspect ratio in all other devices.

Step 4:

What about Device orientation?

There is a function called

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.currentDevice().orientation.isLandscape.boolValue {
            print("Landscape")
            //write your code here such changing autolayout value
        } else {
            print("Portrait")
           // write your code here such changing autolayout value
        }
    }

Utilize this method to handle orientation change.

Note:

If you want to change your Layout for landscape mode you can do it too.No There are many tutorials on how to do it.I think you can google it and find suitable answer.

elk_cloner
  • 2,049
  • 1
  • 12
  • 13