iosinterface-buildersize-classes

How to apply size classes to a UIButton in Xcode


I want my button to have a size of 30x35 on iPhone and 60x70 on iPad. I create the button in a storyboard. I know what I need to use size classes. But I can't create different sizes on different devices. I use this sequence of actions:

  1. Select iPad Pro 9,7 in storyboard.

  2. Create button and change width 60, height 70.

  3. Create width and aspects ratio constraints.

  4. In width constraint unmark Installed press plus and create wR hR Installed.

  5. Select iPhone 8 in storyboard.

  6. Change width 30, height 35.

  7. Create width constraints.

Sometimes I create different sizes on iPhone and iPad but sometimes this doesn't work.

Is this the right sequence of actions or not?

Update

Please tell me if I'm not right. For my button in iPad (60x70) I should create width constraint and press "installed" wR hR. For my button in iPhone (30x35) I should create one more width constraint and press "installed" for wC hC, wR hC, wC hR?


Solution

  • If you just want to vary the width for iphone and ipad, JUST apply size class to your UIButton.

    Go to attributes inspector of your button and click plus button to the left of width property.

    Selected Width: Regular and Height: Regular. and tap on button Add Variation

    enter image description here

    Change the width for ipad (w R h R)

    Now, i my case, width will be 200 for IPhone and 400 for IPad

    applied width

    Note. You don't need to click on installed property even single time.

    Do let me know if you need any help