SwiftiSwift
SwiftiSwift

Reputation: 8747

What´s the difference between positioning a View programmatically with CGRect or with Constraints?

I´m setting up my view programmatically to show up when a button is pressed. My question is what's the difference between positioning a View programmatically with CGRect or with Constraints in storyboard/programmatic constraints?

I tried to set it programmatically and it works fine.

Upvotes: 1

Views: 99

Answers (2)

jon wilson
jon wilson

Reputation: 54

CGRect will not fit all screen sizes as Constraints will. for example. CGRect x: 100, y: 500 may fit a large screen, but for an iPhone SE it will not work because the Item will be off screen. however with constraints like maybe Centre X & Y with a width and height constraint will centre something in the centre of the screen on all devices.

Upvotes: 1

Robert Dresler
Robert Dresler

Reputation: 11190

Simplified:

  • With CGRect you give certain values and UIView will have size and position depending on these values
  • With constraints you give instructions how these values should be created

Upvotes: 2

Related Questions