How to set a UIView's origin reference?
cocoa, ios, uiview, xcode
Solution
After reading these answers and your comments I'm not really sure what is your point.
With `UIView` you can set position by 2 ways:
- `center` – It definitely says it is the center.
- `frame.origin` – Top left corner, can't be set directly.
If you want the bottom left corner to be at x=300, y=300 you can just do this:
UIView *view = ...
CGRect frame = view.frame;
frame.origin.x = 300 - frame.size.width;
frame.origin.y = 300 - frame.size.height;
view.frame = frame;
But if you go one level deeper to magical world of `CALayers` (don' forget to import QuartzCore), you are more powerful.
`CALayer` has these:
- `position` – You see, it don't explicitely says 'center', so it may not be center!
- `anchorPoint` – `CGPoint` with values in range 0..1 (including) that specifies point inside the view. Default is x=0.5, y=0.5 which means 'center' (and `-[UIView center]` assumes this value). You may set it to any other value and the `position` property will be applied to that point.
Example time:
- You have a view with size 100x100
- `view.layer.anchorPoint = CGPointMake(1, 1);`
- `view.layer.position = CGPointMake(300, 300);`
- Top left corner of the view is at x=200, y=200 and its bottom right corner is at x=300, y=300.
Note: When you rotate the layer/view it will be rotated around the `anchorPoint`, that is the center by default.
Bu since you just ask HOW to do specific thing and not WHAT you want to achieve, I can't help you any further now.
Problem
I am creating a UIImageView and adding it in a loop to my view, I set the initial frame to 0,0,1,47 and each passage of the loop I change the center of the image view to space them out. I am always using 0 as the origin.y The problem is the origin reference is in the centre of the image view, assuming we was in interface builder, this is equivalent to the image below. How can I change the reference point in code ?