Search code examples
objective-cuiimageviewuiscreen

Problems setting dimensions of UIImageView


The code below does as follows:

  • detects width and height of screen
  • sets the dimensions of the screen to a UIImageView
  • sets position of the UIImageView to 0,0

Code (being run in viewDidLoad):

NSUInteger widthOfScreen = [UIScreen mainScreen].bounds.size.width;
NSUInteger heightOfScreen = [UIScreen mainScreen].bounds.size.height;
self.background.frame = CGRectMake(0, 0, widthOfScreen, heightOfScreen);

This is being tested in the Simulator on a iPhone 6.

When the code is run, the debugger says the dimension of the screen and UIImageView are 337x568 which is correct (When I implement the correct NSLog statements). But, the simulator shows a UIImageView that does not fit the screen properly. (It is too big). How can I get this to work?

I think it would be important to note that the original dimensions of the image being showed by the UIImageView are 768x1408.


Solution

  • Found the answer:

    http://tripletwollc.com/2013/02/resizing-a-uiimageview-in-viewdidloadviewdidappear/

    According to that website, it seems as if you can't resize a UIImageView in viewDidLoad. It worked when I programmatically created the UIImageView