Reputation: 729
I am getting problem where i am trying to display image exactly in the middle of the screen of UIView *userView.
userView = [[UIView alloc] initWithFrame:CGRectMake(x, y, KVWDT, KVHGT)];//KVWDT=300 & KVHGT=400.
and my image view is to display only on the upper half of the user view, at bottom i am displaying some other fields also
NSString *imageName = [NSString stringWithFormat:[dic objectForKey:@"img"]];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect imgFrame = imageView.frame;
How should i display the image exactly in the middle of userView. Please help me to solve this problem
Upvotes: 0
Views: 135
Reputation: 729
Finally i came up with the solution
CGSize boundsSize = userView.frame.size;
CGRect imgFrameToCenter = imageView.frame;
//center horizontally
if (imgFrameToCenter.size.width<boundsSize.width) {
imgFrameToCenter.origin.x = (boundsSize.width-imgFrameToCenter.size.width)/2;
}
else{
imgFrameToCenter.origin.x=0;
}
Upvotes: 0
Reputation: 8109
use:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeCenter
imageView.center = userView.center;
Upvotes: 1
Reputation: 6710
Give the UIImageView frame the same frame as the view. Then set UIImageView contentMode property to UIViewContentModeScaleAspectFit.
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(x, y, KVWDT, KVHGT)] autorelease];
imageView.image = image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[userView addSubview:imageView];
Upvotes: 0
Reputation: 1806
I think you should use something like
imageView.center = userView.center;
Upvotes: 1