Question

I am having an issue where when I create several UIImageViews programmatically and give them the frame of the main view they end up being half the size. I am building the app in the simulator on a 3.5in retina screen. I checked the frame of the main view and it is (in Landscape) 480pts wide, 320pts high and the origin is at (0,0). It should fill the same part of the screen as the main view (blue in the image below). I'm sure I'm missing something super simple but I just don't see it and searching around for a bit hasn't yielded an answers.

Thanks in advance for your help.

ImageViews as they appear in the simulator

Was it helpful?

Solution

First : You need set contentMode for imageView

imageView.contentMode = UIViewContentModeScaleAspectFill;

Second : Set imageFrame frame by view bounds

imageView.frame = self.view.bounds;

Third : You use Auto layout

OTHER TIPS

There are a few thing to consider:

  • Are you sure your image/imageView contentMode is set properly?

    imageView.contentMode = UIViewContentModeScaleAspectFill;

  • Are you sure you have auto-layout constraints/autoresizing masks are set properly?

  • Have you taken care of screen-rotation?

It'd be helpful if you post some code samples. Otherwise, it's very difficult to find exactly where the problem is.

Hope this helps.

imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];

or

imageView.frame = self.view.bounds;

Also set autoresizing mask either in storyboard or in code In code:

imageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | 
UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleRightMargin | 
UIViewAutoresizingFlexibleLeftMargin | 
UIViewAutoresizingFlexibleHeight | 
UIViewAutoresizingFlexibleWidth;

enter image description here

If you are using auto-layout look at this link Matching subview's width to it's superview using autolayout

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top