Search code examples
iosobjective-cuiimageviewframecgrect

UIImageView initWithFrame not correct size when using self.view.frame


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


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