user3246980
user3246980

Reputation:

Autolayout - Dynamically change subview size depending on screen size

I've got my design in autolayout spacing out correctly but now I'm trying to figure out a way to dynamically change the view sizes for the icons and fonts depending on the screen size (see image below) but none of the setting i've played around with in auto layout seem to strech the images. Is this something I have to do completely programatically? As you can see the iPhone 6 version has way too much space so I need a way to fill that up.

Screen Sizes

Upvotes: 2

Views: 2136

Answers (4)

deltaaruna
deltaaruna

Reputation: 538

I think the best way to address this problem is adding the constraints programmatically. It will give you more control over what you are doing. You can query the screen size and then determine the size of the constrains you are adding. If you need to change the constrains according to the layout orientation, listen to the orientation change then change the constrains. After that you need to call the setNeedsUpdateConstraints method.

I have written this blog article about this problem. There you can get more info about this. Also this github link contains more code example about how to address this.

Upvotes: 0

Kumar KL
Kumar KL

Reputation: 15335

Using size classes with auto layout is just a fun to make your way of UIControls.

I'm trying to figure out a way to dynamically change the view sizes for the icons, Is this something I have to do completely programatically? : No need to do programmatically, You need to understand in depth of the size classes, Just play around with the relationships between the Views, aspect ratio, constraints(siblings) so that which will no conflict other , You can check here for more info.

I'm trying to figure out a way to dynamically change the fonts depending on the screen size : Here you can add this in the attributes inspector on relevant to the size classes, check this from Doc

enter image description here

Upvotes: 0

Oren
Oren

Reputation: 5103

The font sizes you'll have to manually control programmatically. However the image sizes can scale. Just pin the image to its parent but don't constrain in by width/height, instead add the constraint for aspect ratio.

Upvotes: 0

Mike Cole
Mike Cole

Reputation: 1301

You have a couple of options here.

1) You can do it programmatically, but many people screw up the math and the code isn't very clean. The best way to do this (in my opinion) is to set layout constraints on the things you want to change and hook them up with IBOutlets (you can hook up constraints just like you can hook up the UI elements). From there, you can set myConstraint.constant to whatever value you like.

2) You can also set constraints that are <= or >= values. This allows you to have things become larger or smaller up to a limit. You can get crafty with these. Usually you would use a combination of these to set minimum and maximum sizes for things.

3) Xcode 6 allows you to set different constraints for different screen sizes. This is the newest way and probably the way Apple wants you to do it. There should be plenty of info on how to use this option as well.

Upvotes: 2

Related Questions