Here is my image :
Obj *obj = ... obj has imageHref which is NSString
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
Depending on image size I can't figure out how to center it, any ideas ?
This is all inside method :
- (void)drawRect:(CGRect)rect {
Obj *obj = ... obj has imageHref which is NSString
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
NSLog(@"%f", rect.size.width);
NSLog(@"%f", rect.size.height);
}
Where rect.size.width
prints 320.000000 and rect.size.height
prints 416.000000
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.
Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.
This or something very like this should centre horizontally.
CGPoint centerImageView = imageView.center;
centerImageView.x = self.view.center.x;
ImageView.center = centerImageView;
[image drawInRect:CGRectMake((self.view.frame.size.width/2) - (image.size.width/2),
(self.view.frame.size.height / 2) - (image.size.height / 2),
image.size.width,
image.size.height)];
edit:
As you are already in a UIView:
[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];
It is possible to add a macro like this before @interface
in .h class file#define COUNTMID(X,Y) (float) (X-Y)/2
and then use it in code asfloat center COUNTMID(ParentView.frame.width,ChildView.frame.width)
to place your ChildView view at horizontal center of ParentView
I've also made a method, making a ChildView frame to be placed in the center of ParentView:
-(CGRect) CenterFrameOfView:(UIView *) ParentView forView:(UIView *) ChildView
{
CGRect result = CGRectMake(COUNTMID(ParentView.frame.size.width,ChildView.frame.size.width),COUNTMID(ParentView.frame.size.height,ChildView.frame.height),ChildView.frame.size.width,ChildView.frame.size.height);
return result;
}
Or with setFrame
-(void) CenterFrameOfView:(UIView *) ParentView forView:(UIView *) ChildView
{
CGRect result = CGRectMake(COUNTMID(ParentView.frame.size.width,ChildView.frame.size.width),COUNTMID(ParentView.frame.size.height,ChildView.frame.height),ChildView.frame.size.width,ChildView.frame.size.height);
[ChildView setFrame:result];
}
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With