Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a UIView's origin reference?

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 ?

enter image description here

like image 808
Daniel Avatar asked Apr 04 '12 13:04

Daniel


3 Answers

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!
  • anchorPointCGPoint 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:

  1. You have a view with size 100x100
  2. view.layer.anchorPoint = CGPointMake(1, 1);
  3. view.layer.position = CGPointMake(300, 300);
  4. 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.

like image 177
Tricertops Avatar answered Nov 17 '22 02:11

Tricertops


The object's frame includes its position in its superview. You can change it with something like:

CGRect frame = self.imageView.frame;
frame.origin.y = 0.0f;
self.imageView.frame = frame;
like image 8
Phillip Mills Avatar answered Nov 17 '22 02:11

Phillip Mills


If I am understanding you correctly, you need to set the frame of the image view you are interested in moving. This can be done in the simple case like this:

_theImageView.frame = CGRectMake(x, y, width, height);

Obviously you need to set x, y, width, and height yourself. Please also be aware that a view's frame is in reference to its parent view. So, if you have a view that is in the top left corner (x = 0, y = 0), and is 320 points wide and 400 points tall, and you set the frame of the image view to be (10, 50, 100, 50) and then add it as a subview of the previous view, it will sit at x = 10, y = 50 of the parent view's coordinate space, even though the bounds of the image view are x = 0, y = 0. Bounds are in reference to the view itself, frame is in reference to the parent.

So, in your scenario, your code might look something like the following:

CGRect currentFrame = _theImageView.frame;
currentFrame.origin.x = 0;
currentFrame.origin.y = 0;
_theImageView.frame = currentFrame;

[_parentView addSubview:_theImageView];

Alternatively, you can say:

CGRect currentFrame = _theImageView.frame;
_theImageView.frame = CGRectMake(0, 0, currentFrame.size.width, currentFrame.size.height);

[_parentView addSubview:_theImageView];

Either approach will set the image view to the top left of the parent you add it to.

like image 2
jmstone617 Avatar answered Nov 17 '22 03:11

jmstone617