1

I want to change programmatically the ratio of a UILabel to have something adapted between devices (iPhone 4, 5, 6, iPad), because I can't on the StoryBoard:

enter image description here

    if DeviceType.IS_IPHONE_4_OR_LESS {
    } else if DeviceType.IS_IPHONE_5 {
    } else if DeviceType.IS_IPHONE_6 {
    } else if DeviceType.IS_IPHONE_6P {
    } else if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
    }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Ben
  • 761
  • 1
  • 12
  • 35
  • What do you mean by ratio? – koen Jan 27 '16 at 15:05
  • 3
    FYI - The use of a big `if/else` block checking for each device type means you are doing it all wrong. It also means your app won't support multitasking on iPads. – rmaddy Jan 27 '16 at 15:05
  • @rmaddy But I need this to set different sizes like constant constraints – Ben Jan 27 '16 at 15:20
  • @rmaddy Look here: http://stackoverflow.com/questions/35010870/set-constant-for-all-devices?noredirect=1#comment57750937_35010870 – Ben Jan 27 '16 at 15:21
  • Your code should do calculations based on view sizes or size classes, not specific devices. What are you going to do when your app is running in multitasking on an iPad Pro at some size that doesn't match one of your hardcode device checks? Do it the right way. – rmaddy Jan 27 '16 at 15:24
  • @rmaddy But here the calculation is based on the screen size, so it's still like calculations no ? What do you mean to do ? – Ben Jan 27 '16 at 15:33
  • It's fine to base something off of screen size. But your `if/else` is basing it off of specific devices. You should be using size ranges. Something like "if size < 320 ... else if size < 480 ... else if size < 640 ...". The code you have only looks at a few discrete sizes, not ranges of sizes. What happens when Apple adds yet another screen size? Do you want to update countless lines of code because you didn't handle that exact specific screen size? Same for multitasking on iPads. – rmaddy Jan 27 '16 at 15:37
  • @rmaddy that's true, but they're an another problem, managing a lot of constant gonna done a lot of code to set all screen size :( And if one day I want to change some values it gonna be hard – Ben Jan 27 '16 at 15:40
  • I'm simply pointing out a better way than hard coding specific devices. Neither approach should be used at all in most cases. But the code in your question is the worst possible way of all. – rmaddy Jan 27 '16 at 15:41
  • @rmaddy I'm gonna look how to do with ranges sizes – Ben Jan 27 '16 at 15:45
  • @rmaddy Or can we work with percentage ? like constant 90% – Ben Jan 27 '16 at 15:50

1 Answers1

1

The multiplier property (the ratio) is readonly. You should add support for size classes in your constraints by pressing the + sign beside the Installed check box. Thus, you can specify different constraints for different size classes.

enter image description here

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
  • I don't see changes but I think I'm gonna play with leading trailing and height – Ben Jan 27 '16 at 15:19