Reputation: 1251
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...
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
}
}
}
hintLabel.font = UIFont(name: "FontName", size: textSize(.Hint))
heighBetweenLabel1And2.constant = Device.IS_4_INCHES_OR_SMALLER() ? 8 : 15
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
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.
I've given it 3 different height constraints:
>= 30
@ 1000 priority= 32
@ 999 priority= 35
@ 998 priorityThis 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