Toldy
Toldy

Reputation: 1251

Adapt iOS Views/Constraints to all iPhones

I am creating iOS views with Xcode8, I configure them in InterfaceBuilder and most of the time programmatically too.

The views I am dealing with are for each version of iPhone, and iPhone only. The screen sizes are too different from iPhone 4 to iPhone 6s Plus and this makes to job harder.

I know that AutoLayout provides an awesome tool to manage the minimumFont(Scale|Size) but that is not enough for my problem. I want to scale some of the texts sizes and scale some of the constraints accordingly to the screen size.

Maybe I am wrong in my process, but this is what I do for the moment...

  1. I have this code in each View Controller:
enum ButtonType {
    case Title
    case Subtitle
    case Hint
    case Feedback
    case Button
}

func textSize(button: ButtonType) -> CGFloat {
    switch (AppStyleKit.Size.screenSize()) {

    case .Small:
        switch (button) {
        case .Title:
            return 16
        case .Subtitle, .Feedback:
            return 13
        case .Hint:
            return 11
        case .Button:
            return 13
        }
    case .Medium:
        switch (button) {
        case .Title:
            return 17
        case .Subtitle, .Feedback:
            return 14
        case .Hint:
            return 13
        case .Button:
            return 14
        }
    case .Big:
        switch (button) {
        case .Title:
            return 18
        case .Subtitle, .Feedback:
            return 15
        case .Hint:
            return 14
        case .Button:
            return 15
        }
    }
}
  1. I set fontSize of each View in this way:
hintLabel.font = UIFont(name: "FontName", size: textSize(.Hint))
  1. I set the constant of some constraints individually like that:
heighBetweenLabel1And2.constant = Device.IS_4_INCHES_OR_SMALLER() ? 8 : 15 
  1. I have other constraints that are like 2/100 of the Screen Height in AutoLayout. Thus, they are by definition adapted to the screen size.

This works, but that doesn't seem optimal to me. I want to make it easier to maintain. I don't think that having some constraints set in InterfaceBuilder and some other set programmatically is easy to maintain 😕

I know that this question have already been asked by multiple people. I read them but I never found an answer as good as I would like.

Never enough op op :/

Help or even simple advices are welcome!

Upvotes: 1

Views: 148

Answers (1)

Kumuluzz
Kumuluzz

Reputation: 2012

Try to set the same constraint several types on the same element, but changing the priority of each constraint.

As an example: a UIButton in the center of the screen.

A <code>UIViewController</code> with a <code>UIButton</code> in the center that has 3 height constraints.

I've given it 3 different height constraints:

  • A height of >= 30 @ 1000 priority
  • A height of = 32 @ 999 priority
  • A height of = 35 @ 998 priority

This configuration will ensure that the button is always at least 30 points height, but it will try to expand to either 32 or 35 if there's enough space on the screen.

Now the concrete setup depends on your case of course, but if you want to change the font sizes as well, then try to experiment using a combination of UIEdgeInsets and the minimum font size to control the area on which the text is actually drawn.

Upvotes: 2

Related Questions