UIImageview won't center in superview
Asked Answered
H

3

6

I have an UIImageView which I add to a UIView to enhance the area where touches are recognized. When I try to center the UIImageView in the UIView (code below) the center is set properly but when I run the program the image is shown way off outside the frame of the UIView.

Any ideas?

// image size is 32x32 pixels but view will be created with 44x44 so touch recognition is better
self = [super initWithFrame:CGRectMake(position.x, position.y, 44, 44)];

if (self) {
    switch (color) {
        case Blue:
            self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_red"]];
            break;
        case Green:
            self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_green"]];
        default:
            break;
    }

    [self setCenter:position];
    [self addSubview:self.imageView];
    [self setExclusiveTouch:YES];        
    [self setBackgroundColor:[UIColor clearColor]];

    [self.imageView setCenter:position];

    NSLog(@"uiview center: %@", NSStringFromCGPoint(self.center));
    NSLog(@"image center: %@", NSStringFromCGPoint(self.imageView.center));

Thanks!

Hindoo answered 7/11, 2012 at 13:45 Comment(0)
C
21

The imageview setCenter is relative to its parent. Try:

[self.imageView setCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)];
Commemoration answered 7/11, 2012 at 13:53 Comment(5)
Thanks for the quick answer but if i try [self.imageView setCenter:self.center] the result is the same :-(Hindoo
Thats not what I was suggesting. You need to get a point that is relative to the parent. self.center will return it relative to its parent, and shouldn't workCommemoration
but self is the superview and so it's the parent in this case. Or am i wrong?Hindoo
It will report it in its parent coordinates. There is always UIWindow at the very top levelCommemoration
ahh, now i got it :-) [self.imageView setCenter:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))]; did the trick! thanks!Hindoo
B
1
UIImageView *imageforFace=[[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2, 125,125)];
imageforFace.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[imageforFace setCenter:CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2)];
imageforFace.image=[UIImage imageNamed:@"face.png"];
[self.view addSubview:imageforFace];

Final the output is

enter image description here

Butadiene answered 28/5, 2015 at 14:41 Comment(0)
E
0

try this code to give the UIImageView Frame Centre of UIView

1) in .h file

UIImageView *imgCenter;

2) in .m file

- (void)viewDidLoad
{
    imgCenter=[[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2, 35, 35)];
    [imgCenter setImage:[UIImage imageNamed:@"centerImage.png"]];
    [self.view addSubview:imgCenter];
}

OR

- (void)viewDidLoad
{
    imgCenter=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
    imgCenter.center = self.view.center;
    [imgCenter setImage:[UIImage imageNamed:@"centerImage.png"]];
    [self.view addSubview:imgCenter];
}
Eligibility answered 2/12, 2014 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.