0

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.

Minestrone-Soup
  • 399
  • 1
  • 16
  • Have you try clipping the UIImageView to subviews? – Faiz Mokhtar Dec 23 '14 at 01:10
  • @faizmokhtar I'm not quite sure what that means, so the obvious answer is know. It sounds like your going to end up cutting the `UIImageView` into pieces, and I'd rather have one whole – Minestrone-Soup Dec 23 '14 at 01:16
  • Is `self.background` the `UIImageView ` ? Did you use auto-layout ? If not, what is the `autoresizingMask` of the `UIImageView` ?. – KudoCC Dec 23 '14 at 01:46
  • Can you show more code. Where is this code snippet being run? Is it in viewDidLayoutSubviews? Are you running super first? Also, did you set the content mode to UIViewContentModeScaleAspectFill? – Fogmeister Dec 23 '14 at 01:58
  • @KudoCC Ya, I realized I didn't need that cause I'm not using constraints. Got rid of it. `background` is the `UIImageView`. – Minestrone-Soup Dec 23 '14 at 02:13
  • @Fogmeister It is being run in viewDidLoad, and this is basically the entire bit of code I have – Minestrone-Soup Dec 23 '14 at 02:13

3 Answers3

0

It's probably because your image is too big and it is not clipping to the parent view. Try clipping the image first to fit the UIImageView.

self.view.clipsToBounds = YES.

You can refer about this from here: why is my UIView subview not rendering within the parent? (code attached)

Also some tips, try changing the background color of the UIImageView to red or something so that you can clearly how your view would pan out.

Community
  • 1
  • 1
Faiz Mokhtar
  • 938
  • 1
  • 10
  • 24
0

Firstly, you should push your code inside viewWillLayoutSubviews of your vc. If the problem is still, check the contentMode of your imageView and make sure to change it to UIViewContentModeScaleToFill or UIViewContentModeScaleAspectFit if your image is too large.

sahara108
  • 2,829
  • 1
  • 22
  • 41
0

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

Minestrone-Soup
  • 399
  • 1
  • 16