Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align my image in the center IOS horizontally

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

like image 908
London Avatar asked Feb 28 '12 15:02

London


People also ask

How do I align an image horizontally in center?

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.

How do you centralize an image?

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.

How do I center an image horizontally in CSS?

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.


3 Answers

This or something very like this should centre horizontally.

CGPoint centerImageView = imageView.center;
centerImageView.x = self.view.center.x;
ImageView.center = centerImageView;
like image 50
Damo Avatar answered Nov 11 '22 04:11

Damo


[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)];
like image 31
Melvin Avatar answered Nov 11 '22 03:11

Melvin


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 as
float 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

like image 43
ETech Avatar answered Nov 11 '22 03:11

ETech