Question

I know there are lots of similar questions here and I checked the famous one, and then grasped the difference between Bounds, and Frame.

Now I have some problem related to them. I played around with them , but it didn't show as I expected.

What I don't understand here is:

  • Why the frame origin of Y is 44.000000 below the top even I set the UIImageView at the left corner? Because bounds should be "The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0)." (Cocoa: What's the difference between the frame and the bounds?) I thought the frame also should start at the left corner here.

enter image description here

#import "ViewController.h"

@interface ViewController ()

//@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UIImageView *image2;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


//UIImage *imageView = [UIImage imageNamed:@"stanford"];

//    self.image.alpha = 1;
//    self.image.contentMode = 1;
//    self.image.image = imageView;
//    [self.image setAlpha:0.1];
//    [self.image setContentMode:UIViewContentModeCenter];
//    [self.image setImage:imageView];



    NSURL *url = [[NSURL alloc] initWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Pitbull_2%2C_2012.jpg/472px-Pitbull_2%2C_2012.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *imageData = [UIImage imageWithData:data];

    [self.image2 setBounds:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.image2 setImage:imageData];

    NSLog(@"frame.orign.x:%f,frame.origin.y:%f",self.image2.frame.origin.x,self.image2.frame.origin.y);







}
Was it helpful?

Solution

The 44 magic number value actually comes from the navigation bar :) Whereas the height of the status bar is 20 points.

If you want your image to cover the entire screen then you will need to either get rid of your status bar, or make your status bar translucent so that content can be displayed underneath.

If you don't set your status bar/navigation bar to translucent, then the point of origin 0,0 would start just underneath the bars as you are experiencing now.

status bar is set using

[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];

The navigation bar, if you have one displayed can be set using this

theNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

Only then will your following code display your content across the full screen

[self.image2 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]

This is because setting the bars to translucent will have have them displayed as overlays instead.

OTHER TIPS

The 44 comes from the nav bar which takes up that much height

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