Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a CGRect from a CGPoint and CGSize?

I need to create a frame for a UIImageView from a varying collection of CGSize and CGPoint, both values will always be different depending on user's choices. So how can I make a CGRect form a CGPoint and a CGSize? Thank you in advance.

like image 621
inorganik Avatar asked Aug 21 '12 21:08

inorganik


2 Answers

Two different options for Objective-C:

CGRect aRect = CGRectMake(aPoint.x, aPoint.y, aSize.width, aSize.height);  CGRect aRect = { aPoint, aSize }; 

Swift 3:

let aRect = CGRect(origin: aPoint, size: aSize) 
like image 138
Jim Avatar answered Sep 20 '22 02:09

Jim


Building on the most excellent answer from @Jim, one can also construct a CGPoint and a CGSize using this method. So these are also valid ways to make a CGRect:

CGRect aRect = { {aPoint.x, aPoint.y}, aSize }; CGrect aRect = { aPoint, {aSize.width, aSize.height} }; CGRect aRect = { {aPoint.x, aPoint.y}, {aSize.width, aSize.height} }; 
like image 25
coco Avatar answered Sep 22 '22 02:09

coco