0

I'm adding a UIImageView to the UIViewController's UIView as I would normally, with the frame of the image view being the same as self.view to make sure that the image will cover the whole view. However, there is a roughly 20px padding between the status bar and the imageView which I can't seem to get rid of.

I've tried NSLogging the different frames, and receiving the following results:

 NSLog(@"ImageView => %@", NSStringFromCGRect([imageView frame]));
 NSLog(@"self.view => %@", NSStringFromCGRect(self.view.frame));
 NSLog(@"Screen Bounds => %@", NSStringFromCGRect([[UIScreen mainScreen] bounds]));
 NSLog(@"App window => %@", NSStringFromCGRect([[[[UIApplication sharedApplication] delegate] window] frame]));

[LaunchViewController.m: line 26] ImageView => {{0, 0}, {320, 548}} 
[LaunchViewController.m: line 27] self.view => {{0, 0}, {320, 548}} 
[LaunchViewController.m: line 28] Screen Bounds => {{0, 0}, {320, 568}}   
[LaunchViewController.m: line 29] App window => {{0, 0}, {320, 568}}

The results show that the image view should in fact be covering self.view with no padding at all between itself and the status bar.

The UIViewController is the root view controller of a UINavigationController, and I have set the navigation bar to be hidden. I thought that maybe this could be causing the issue however, after removing the UINavigationController and replacing it with just the UIViewController, the padding was still there.

Has anybody got any advice for removing the padding / has experience this issue before and has a fix?

Here is an image showing what's going on - the green area is the UIView (self.view), and the orange area is the UIImageView.

enter image description here

[self.navigationController setNavigationBarHidden:YES];
[self.view setBackgroundColor:[UIColor greenColor]];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageView setImage:[UIImage imageNamed:@"Orange-568h@2x.png"]];
[self.view addSubview:imageView];
max_
  • 24,076
  • 39
  • 122
  • 211

7 Answers7

1

I had the same problem and it solved with this:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

self.view.frame give you the wrong x and y when status bar is not hidden

Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • If you have a look at the code I posted, you'll find that the frame is exactly the same as that. – max_ May 26 '13 at 14:40
  • self.view.frame give you the wrong x and y when status bar is not hidden, try it and see – Mutawe May 26 '13 at 14:43
1

In your app delegate set the FullScreenLayout to YES just before you make it the view controller the root view controller:

ViewController *vc = [[ViewController alloc] init];
[vc setWantsFullScreenLayout:YES];
[[self window] setRootViewController:vc];

or you can set it in viewDidLoad

[self setWantsFullScreenLayout:YES];

A summary on setWantsFullScreenLayout: When a view controller presents its view, it normally shrinks that view so that its frame does not overlap the device’s status bar. Setting this property to YES causes the view controller to size its view so that it fills the entire screen, including the area under the status bar. (Of course, for this to happen, the window hosting the view controller must itself be sized to fill the entire screen, including the area underneath the status bar.) You would typically set this property to YES in cases where you have a translucent status bar and want your view’s content to be visible behind that view. From here

Hope this helps.

Iain Smith
  • 9,230
  • 4
  • 50
  • 61
1

See your self.view, it is 20 points below, as the status bar is shown. hence, the self.view.frame is (0,20,w,h)

your image view takes the same rect (0,20,w,h) and adds it to self.view. With respect to self.view the image view sets its frame to 20 points below the "y" position of self.view.

Try setting self.view.bounds to the image view frame.

Hope this helps!

Nikita P
  • 4,226
  • 5
  • 31
  • 55
0

Try UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; isntead. Using bounds instead of frame

Remover
  • 1,616
  • 1
  • 17
  • 27
  • Try not setting the image and instead set the backgroundColour of your imageView so you can be sure about the frame that you're using. – Remover May 26 '13 at 13:54
0

on the viewDidLoad method your UIViewController's xib is loaded and your updated values regarding your User Interface is not detected.

This causes no trouble if your UIView is exactly same (no UINavigationBar, UITabbar, status bar or device -iPhone 3.5 inch / 4 inch change). But if you are changing anything in the process, you might get false values on viewDidLoad.

Easiest way to check this is to debug your screen and view bounds on the viewDidAppear, after UI gets updated.

Please check your xib/storyboard. There is a change between your current UI and designed interface.

To solve this you might use a function updateUI on the viewDidAppear and update related view's again.

[self.imageView setFrame:self.view.frame]

this will fix it.

Bartu
  • 2,189
  • 2
  • 26
  • 50
0

Try move [self.navigationController setNavigationBarHidden:YES]; to

- (void) viewWillAppear: (BOOL) animated
{
    [super viewWillAppear: animated];    

    [self.navigationController setNavigationBarHidden: YES
                                             animated: NO];
}
stosha
  • 2,108
  • 2
  • 27
  • 29
-1

Try to change self.view.frame to self.view.bounce when you add imageView into self.view.

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

Or just use CGRectMake(0,0,320,568)

andykkt
  • 1,696
  • 16
  • 23
  • It needs to be dynamic I'm afraid. The app will not only be running on the iPhone 5. – max_ May 26 '13 at 13:51
  • this is forcing a CGRect, not a solution. I am sure OP could have figured it out all by himself. Yet he is seeking why is this happening – Bartu May 26 '13 at 13:51
  • http://stackoverflow.com/questions/1054539/not-sure-why-uiview-is-being-nudged-up-by-around-10px – andykkt May 26 '13 at 14:00