Caleb Kleveter
Caleb Kleveter

Reputation: 11494

Constraints that are relative to screen size

Is there a way to make constraints change based on the screen size of your device, like percentages in CSS? I have a grid of images that will look good on only on device. Will CollectionViewController fix my problem, or maybe testing for screen size and linking to a different Storyboard depending on the screen size?

Upvotes: 3

Views: 6023

Answers (2)

simardeep singh
simardeep singh

Reputation: 220

These 4 constraints change the objects according to screen sizes, but for that you shouldn't give any width or height constraint. Top edge,Leading edge,Trailing edge,Bottom edge. Now you can check how it will look by selecting the view and clicking Assistant Editor,then Automatic, then Preview. There will be +sign at the bottom of screen, click on that and add the devices on which you want to check.

Upvotes: 1

user5132172
user5132172

Reputation:

Using Storyboard constraints and programatically

There are many approaches, this is only the approach I use:

1) Add constraint in Storyboard

enter image description here

.

2) Access constraint in View Controller programatically (Drag and Drop in Assistant editor)

enter image description here

3) Change constraint value dependant on screen size

let screenSize: CGRect = UIScreen.mainScreen().bounds
imWidth.constant = screenSize.width * 0.85 // Make width 85% of Screen Width

Upvotes: 9

Related Questions